0
votes

I created a user control, where I am providing design-time support. I expose appropriate properties and my required child controls are being correctly persisted in the parent form that contains the user control.

In the form's load event, I need to re-construct certain child controls from the "MenuItems" property (see code below) that got persisted. The Load event fires correctly every time the form is opened in Visual Studio IDE. The first time the form is opened the collection correctly contains all expected items. However, when I open the form again later in the same Visual Studio session, the collection is empty. The Load event fires, but the persisted collection is empty. If I close Visual Studio and open the form again, the collection correctly shows the expected number of items again.

Below is the relevant code. I am using a 3rd party DevExpress NavBarControl, where I rebuild the menu items from a persisted collection. The items are code generated into the parent's form (MenuItems is the collection that gets persisted):

public partial class MyUserControl : UserControl
{
    private List<NavBarItem> menuItems = new List<NavBarItem>(15);

    [Browsable(false),
       DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<NavBarItem> MenuItems
    {
       get
       {
           return this.menuItems;
       }
    }

    public MyUserControl()
    {
        InitializeComponent();
    }

    private void NavOptionsControl_Load(object sender, EventArgs e)
    {
        foreach (NavBarItem item in this.menuItems)
        {
            NavBarItemLink link = new NavBarItemLink(item);
            this.navBarGroup.ItemLinks.Add(item);
            item.LinkClicked += new NavBarLinkEventHandler(NavBarItem_LinkClicked);
        }
    }
}
1
Just for verification, you do dispose of the form, and create a new one later, correct?user978122
My question is about design-time, not run-time. I am talking about opening the parent form in Visual Studio Designer, where this parent form contains my user control.Elan

1 Answers

1
votes

Have you tried the layout event? or possibly the paint event?