This does not work. Below are some code snippets of what I am doing:
// The ZoneItems class
public partial class DynamicForm : Form
{
#region Zone Collection Class
/// <summary>
/// Zone collection class.
/// </summary>
public class ZoneItems : CollectionBase
{
#region Constructor
// Define in here!
#endregion
#region Methods
/// <summary>
/// Gets or sets a zone by index.
/// </summary>
/// <param name="index">int: index of zone.</param>
/// <returns>ZonePanel: zone at index.</returns>
public ZonePanel this[int index]
{
get { return ((ZonePanel)List[index]); }
set { List[index] = value; }
}
/// <summary>
/// Adds a zone to the collection.
/// </summary>
/// <param name="zone">ZonePanel: zone to add.</param>
public void Add(ZonePanel zone)
{
List.Add(zone);
// Raise the zone added event.
this.raiseZoneAddedEvent(new EventArgs());
}
#endregion
#region Events
// Create an event delegate and an event.
public delegate void ZoneAddedEventHandler(object sender, EventArgs e);
/// <summary>
/// Occurs when a zone is added.
/// </summary>
public event ZoneAddedEventHandler ZoneAdded;
// Wrap raising the event in a method.
protected void raiseZoneAddedEvent(EventArgs e)
{
if (ZoneAdded != null)
{
ZoneAdded(this, e);
}
}
#endregion
}
#endregion
}
// The custom form class
public partial class DynamicForm : Form
{
protected ZoneItems pZoneRegion;
...
#region Event Handlers
protected virtual void Items_ZoneAdded(object sender, EventArgs e)
{
this.Controls.Add(((ZoneItems)sender)[((ZoneItems)sender).Count - 1]);
}
#endregion
...
#region Properties
[Category("Design")]
[Description("Adds zone regions to the forms zone collection.")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public virtual ZoneItems ZoneRegion
{
get
{
return this.pZoneRegion;
}
set
{
this.pZoneRegion = value;
}
}
#endregion
}
So in DESIGN TIME the ZoneItems added do not appear on the form until the form is closed in VS and re-opened.