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;
}
}
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:
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]