Desing Time: Initialize the Collection Editor.

I have a control that adds two properties, when used: a collection (say MyCollection) and a bool (HasInitialValue). I'm using CollectionEditor to edit the collection. Everything works fine (collection items are components and the designer is CollectionEditor) as long as I add/remove items through the CollectionEditor UI.

Now the idea is: At design time, if I set the HasInitialValue property of the control to true, I want to add an initial item to the collection. I can add it into the inner collection MyCollection, I can see it in the CollectionEditor but there's no generated code for it in the form initializer (that uses the control) and it's lost after build.

Any ideas? I must somehow trigger the action of the Add button of MyCollection CollectionEditor   when I change the other property and add the item to the inner collection...
LVL 9
ripahoratiuAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
discon0Connect With a Mentor Commented:
Add the following code in the setter of HasInitialValue, right after the MyCollection.Add(xcol); line.

            if (this.Container != null)
            {
                  this.Container.Add(xcol);
            }

This will add the new item (component) to the designer and it will generate code and persist it's values.
0
 
discon0Connect With a Mentor Commented:
You probably already have a member variable like this:

            private bool hasInitialValue = false;

and an associated property that you set/unset with the designer.

When you change the value of this property the designer writes code in the control that contains your control, like this:

                  this.myControl1.HasInitialValue = true;

All you need to do is have your controls property add or remove the initial value on the set method. This way it will work in design time and run time as well, since the designer generated code will first create your control, then set the HasInitialValue property which will in turn add your value to the collection.
Example:

            [DefaultValue(false)]
            public bool HasInitialValue
            {
                  get { return (hasInitialValue); }
                  set
                  {
                        if (value != hasInitialValue)
                        {
                              if (value)
                              {
                                    myCollection.Add("My Initial Value");
                              }
                              else
                              {
                                    myCollection.Remove("My Initial Value");
                              }
                              hasInitialValue = value;
                        }
                  }
            }
0
 
ripahoratiuAuthor Commented:
I've done that. Here's the control code:

    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
        }

        private List<MyItem> myCollection = new List<MyItem>();
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public List<MyItem> MyCollection
        {
            get { return myCollection; }
        }

        public class MyItem : Component
        {
            private String prop1;
            public String Prop1
            {
                get { return prop1; }
                set { prop1 = value; }
            }

            private Int32 prop2;
            public Int32 Prop2
            {
                get { return prop2; }
                set { prop2 = value; }
            }

        }

        private bool hasInitialValue;
        public bool HasInitialValue
        {
            get { return hasInitialValue; }
            set
            {
                if (value)
                {
                    MyCollection.Clear();
                    MyItem xcol = new MyItem();
                    MyCollection.Add(xcol);
                }
                hasInitialValue = value;
            }
        }
    }

Now if you put it on a form, set the HasInitialValue to true. The CollectionEditor will contain a new item. Set some values for the item and return check the form: you won't see any item add in the initializer. Add a new Item via UI. You'll see it's code in forms initializer. Basically the item is re-created (with default values) after rebuild, it is not persisted.
0
 
ripahoratiuAuthor Commented:
Great! It's working! 10000000000000 thanks.

Now if you tell me how can I disable the Add/Remove button of CollectionEditor, once populated with the initial value (to stop other items to be added or the initial item to be removed) that would be excellent.
0
 
miztakenCommented:
hello
I need to disable Add/Remove button of CollectionEditor. Please help me out. Did you got ur solution. if you have any i would be very great to you



0
All Courses

From novice to tech pro — start learning today.