Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a bit of problem in adding events on controls created by the custom collection editor. The following is my code for user control:

public partial class User : UserControl
{
    public ButtonsCollection m_MultipleItems;

    [Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [MergableProperty(false)]
    [Description("Contains ListView, Button and CheckBox items in a List<Control> collection")]
    [Category("Behaviour")]
    public ButtonsCollection _MultipleItems
    {
            get
            {
                if (m_MultipleItems == null)
                    m_MultipleItems = new ButtonsCollection(this);
                return m_MultipleItems;
            }
    }

    //collectioneditor class
    public class CustomCollectionEditor : CollectionEditor
    {
        public CustomCollectionEditor()
            : base(typeof(ButtonsCollection))
        {
        }

        protected override object CreateInstance(Type itemType)
        {
            Control control = base.CreateInstance(itemType) as Control;
            if (itemType == typeof(Button))
            {
                control.Text = "My Button";
            }

            else if (itemType == typeof(Label))
            {
                control.Text = "My Label";
            }

            else if (itemType == typeof(CheckBox))
            {
                control.Text = "My CheckBox";
            }

            else if (itemType == typeof(TextBox))
            {
                control.Text = "My TextBox";
            }

            return control;
        }

        protected override string GetDisplayText(object value)
        {
            Control control = value as Control;
            return string.Format("{0} - {1}", control.GetType().Name, control.Text);
        }

        protected override Type[] CreateNewItemTypes()
        {
            return new Type[] { typeof(TextBox), typeof(Button), typeof(CheckBox), typeof(Label) };
        }
    }
}


And this is my code for My collection:

C#
public class ButtonsCollection : CollectionBase
    {
        Control m_Parent;

        public ButtonsCollection(Control Parent)
        {
            m_Parent = Parent;
        }

        public virtual Control this[int index]
        {
            get { return (Control)List[index]; }
        }

        protected override void OnInsert(int index, object value)
        {
            base.OnInsert(index, value);
            m_Parent.Controls.Add((Control)value);
        }

        protected override void OnRemove(int index, object value)
        {
            base.OnRemove(index, value);
            m_Parent.Controls.Remove((Control)value);
        }
}

Where exactly do I add the event?
E.g.
I used the usercontrol and placed it in a windows form, then I add a button using the collection editor, how do I do it such that when I add a button, it will automatically have an event, where to code adding event? In run time this event could be used. Like click for button etc..

[edit]SHOUTING removed - OriginalGriff[/edit]
Posted
Updated 21-May-12 22:46pm
v2
Comments
OriginalGriff 22-May-12 4:46am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
BillWoodruff 23-May-12 7:17am    
< note: I posted this, by mistake, as an "answer," when it should have been posted as a comment, since it just seeks clarification from the OP: hence it has been deleted as "answer," and is now here >

This is an interesting question partially because I cannot visualize exactly how a new Control, which may be Button, Label, CheckBox, etc., is added.

Clearly if you are wanting to define some Event which every type of Control you create has a handler for, like "Click," that's one thing, and if you want to define an Event that is unique to one type of Control only ... a TextBox, for example: that's another thing.

You'll have to convert/cast somewhere your Control which is actually a TextBox, into a valid TextBox to allow you to attach EventHandlers specific to TextBox to it.

What is your goal in creating a collection of Controls of several different types here ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900