Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi,

I have a custom form (derived from the System.Windows.Forms class) whereby I can add a number of ZonePanel user controls (derived from the Panel control) which will split the form up into Zones (similar to that of the ASP.NET Zone control). I have a property on the custom form which is actually a collection of the type ZonePanel, whereby I can add the ZonePanel to the form.

This is OK and works fine by bringing up the ZonePanel collection editor. I can add any number of ZonePanel controls and set their properties, however, when I click OK, the ZonePanels are not shown on the form unless I close the form and reopen it in Visual Studio.

I am suspecting I need to create a designer component model for the custom form, but I don’t know what I need to implement to cause the form to update it’s child controls.

Incidentally, the FormX_Designer.cs form contains the number of controls that I added in the collection.

Also, as another related question, is there a way to either prevent a control from being deleted from the design surface (i.e. only allow the control to be removed from the collection editor) or conversely update the collection in the collection editor.

Any help would be most appreciated.
Thanks
Andy
(MCTS)
Posted
Updated 11-Feb-11 0:09am
v2

Your first question...

You may somehow need to call Invalidate in the host Form when you are adding to your collection. Perhaps something like:
C#
if(null != this.Parent)
    this.Parent.Invalidate();

at the end of your Add, AddRange, Insert etc methods?
 
Share this answer
 
v2
This does not work. Below are some code snippets of what I am doing:


// The ZoneItems class
XML
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

C#
public partial class DynamicForm : Form
{

// Property related fields.
protected ZoneItems pZoneRegion;

...

#region Event Handlers
// Handle the items zone added event handler.
protected virtual void Items_ZoneAdded(object sender, EventArgs e)
{
    // Add the zone to the forms control collection.
    this.Controls.Add(((ZoneItems)sender)[((ZoneItems)sender).Count - 1]);
}
#endregion


...


#region Properties
/// <summary>
/// Gets or sets the zone control collection.
/// </summary>
[Category("Design")]
[Description("Adds zone regions to the forms zone collection.")]
// Must serialize contents of the panel since it holds controls.
[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.
 
Share this answer
 
v3
Comments
DaveyM69 11-Feb-11 9:53am    
As this isn't an answer you should have edited your original question to include the code - not posted as an answer!

Have youtried adding a call to Invalidate(); after this.Controls.Add(((ZoneItems)sender)[((ZoneItems)sender).Count - 1]);
Invalidate works in DesignMode too.

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