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 点击事件定义中.
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.
这篇关于如何使用委托和事件处理程序进行用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用委托和事件处理程序进行用户控制
基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
