To wrap it up, I found a solution to my problem. Like a TabControl, my ButtonList contains two collections:
- ControlCollection inherited from the Control class
- ButtonListItemCollection (TabControl has TabPageCollection)
Adding ButtonListItems is done via the collection editor in the properties. Removing can be done either in the collection editor or just by selecting the ButtonListItem in the designer and pressing the DEL button on the keyboard (manual deletion).
When using the collection editor, ButtonListItems are added/removed to/from the ButtonListItemCollection and then to/from the ControlCollection.
However, when a ButtonListItem is manually deleted it is only deleted from the ControlCollection not from the ButtonListItemCollection. So what needs to be done is to detect when the ButtonListItemCollection holds ButtonListItems which are not in the ControlCollection.
Unfortunately in the Compact Framework there is no such thing as a ControlRemoved event in the Control class. So one solution would look like this:
class ButtonList : ContainerControl
{
// ...
public ButtonListItemCollection Items { get; private set; }
protected override void OnPaint(PaintEventArgs pe)
{
if (this.Site.DesignMode)
{
this.Items.RemoveAll(x => !this.Controls.Contains(x));
}
}
// ...
}
Hope that helps anyone working with the compact framework.