简体   繁体   中英

Catch event when remove user control from panel

I wrote a user control. I have a panel, when I click on the button in user control it is added to the panel. I have created a click event for user control. When clicked, the user control is removed from the panel. I want to ask that to catch the event when the user control is removed, which event should I use and use for panel or user control. For example, I add a label, when adding user control the label will change, and when deleting, the label will also change. Sorry for my bad English.

The event you are most likely to find helpful is the ControlRemoved event of the Control .

This is described in the docs thus:

Occurs when a control is removed from the Control.ControlCollection.

In the case of your example, you would add an event handler for this to the Panel . Assuming your panel is named panel1 (please don't name it that) this would be something like this:

// In initialisation code somewhere
panel1.ControlRemoved += panel1_ControlRemoved;

private void panel1_ControlRemoved(object sender, ControlEventArgs e)
{
    // Do something
    ...
    // Note: removed control is referenced by e.Control
}

This would be raised anytime a control is removed from the Panel .

There's also an accompanying event for when a control is added which is named ControlAdded .

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