如何与代表和事件处理程序的用户控制工作 [英] How to work with delegates and event handler for user control
问题描述
我已经创建了一个包含一个按钮的用户控件。我使用我的WinForm该控件将在运行时从数据库中获取数据后,被载入
I have created a user control that contains a button. I am using this control on my winform which will be loaded at run time after fetching data from database.
现在我需要从数据表中删除一行该按钮的Click事件。
Now I need to remove a row from a datatable on the Click event of that button.
的问题是,我怎么抓住我的形式事件。目前,它会在用户控件的BTN click事件认定中。
The problem is that how do I capture that event in my form. Currently it goes in that user control's btn click event defination.
推荐答案
您可以通过执行中的以下创建自己的委托事件您用户控制:
You can create your own delegate event by doing the following within your user control:
public event UserControlClickHandler InnerButtonClick;
public delegate void UserControlClickHandler (object sender, EventArgs e);
您使用以下调用从处理事件:
You call the event from your handler using the following:
protected void YourButton_Click(object sender, EventArgs e)
{
if (this.InnerButtonClick != null)
{
this.InnerButtonClick(sender, e);
}
}
然后你可以使用挂接到事件
Then you can hook into the event using
UserControl.InnerButtonClick+= // Etc.
这篇关于如何与代表和事件处理程序的用户控制工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!