简体   繁体   English

从面板中删除用户控件时捕获事件

[英]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 .您最有可能发现有帮助的事件是ControlControlRemoved事件。

This is described in the docs thus:这在文档中描述,因此:

Occurs when a control is removed from the Control.ControlCollection.从 Control.ControlCollection 中删除控件时发生。

In the case of your example, you would add an event handler for this to the Panel .在您的示例的情况下,您将为此添加一个事件处理程序到Panel Assuming your panel is named panel1 (please don't name it that) this would be something like this:假设您的面板名为panel1 (请不要命名),这将是这样的:

// 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 .任何时候从Panel删除控件都会引发此Panel

There's also an accompanying event for when a control is added which is named ControlAdded .添加控件时还有一个伴随事件,名为ControlAdded

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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