简体   繁体   中英

UserControl Load event raised once in Winform

I have a Winform with a panel that loads different user controls depending on user inputs.

// to load a user control
pnlContent.Controls.Add(uc1);

// to change to different user control
pnlContent.Controls.Clear();
pnlContent.Controls.Add(uc2);

What I notice is that when I clear pnlContent and re-add uc1, the uc1's Load event doesn't get triggered, ie uc1's Load event only gets triggered in the very first time uc1 gets added to the pnlContent.

Is there a way to have the Load event always triggered every time the user control gets added to the panel? If not, what are my options?

Please advise.

  pnlContent.Controls.Clear();

You have to be very careful with this method, it doesn't do what you think it does. It does not dispose the controls on the panel, it merely removes them. The controls go to live on, their windows are hosted to the hidden "parking window". Ready to be moved back to another parent.

In many cases that does not happen and the control will leak forever. In your specific case it isn't quite that bad yet, you still have a reference to the control. Your uc1 variable stores it. The consequence however is that its Load event doesn't fire again, that only happens once.

So if you really need that Load event to fire then you should do this the proper way, actually dispose the controls on the panel:

 while (pnlContent.Controls.Count > 0) pnlContents.Controls[0].Dispose();

And then you have to create a new instance of whatever usercontrol type uc1 references. You'll then get the Load event to fire when you add it to the panel.

Another strong hidden message in this answer is that it is very likely that you shouldn't be using the Load event at all. In the vast majority of cases, code in the Load event handler belongs in the constructor. You only need Load if you need to know the Handle property or need to be sure that layout was calculated so that the final size of the control is known. That's rare.

I have a control that acts like a toolbox, which is added and removed from another parent control as the user push a toggle button.

Instead of using the Load event (that triggers only the first time the control is added to its parent controls collection), I use the ParentChanged event.

Then I check the Parent property: if it is null, that means the control has been removed from its parent collection. If it is not, the control has just been added.

private void MyUserControl_ParentChanged(object sender, EventArgs e)
{
    try
    {
        if (this.Parent != null) 
        {
            // Perform initializations
        }
    }
    catch (Exception ex)
    {
        // Do my normal exception handling
    }
}

I think you can do:

uc1.load(this, null);

Because you only remove it, and add it again. You dont reinitialize it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM