Question

I am writing a custom control for Windows Mobile 6 devices with the Compact Framework 3.5 in C#. The control I am writing represents a list of buttons, let's call it ButtonList and the buttons ButtonListItem.

The list of ButtonListItem is editable in the designer through the collection editor:

Visual Studio Designer Items Collection

I set it up so that the ButtonList is updated and repainted when a ButtonListItem is added or removed in the collection editor.

Here is the problem:

When I delete a ButtonListItem manually by selecting it with the mouse and pressing the delete button in the designer, the the internal state of the ButtonList is not updated.

My goal is to make the manual deletion in the designer behave like a removal in the collection editor.

An example would be the TabControl class where removal of tab pages in the collection editor and manual deletion behave exactly the same.

How would you achieve this?


Edit 1: Simplified code for adding and removing a ButtonListItem to the ButtonList

class ButtonList : ContainerControl
{        
    public ButtonListItemCollection Items { get; private set; } // Implements IList and has events for adding and removing items

    public ButtonList()
    {
        this.Items.ItemAdded += new EventHandler<ButtonListItemEventArgs>(
            delegate(object sender, ButtonListItemEventArgs e)
            {
                // Set position of e.Item, do more stuff with e.Item ...

                this.Controls.Add(e.Item);   
            });

        this.Items.ItemRemoved += new EventHandler<ButtonListItemEventArgs>(
            delegate(object sender, ButtonListItemEventArgs e)
            {
                this.Controls.Remove(e.Item); 
            });
    }
}
Was it helpful?

Solution

To wrap it up, I found a solution to my problem. Like a TabControl, my ButtonList contains two collections:

  1. ControlCollection inherited from the Control class
  2. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top