using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace UserControls { public partial class UserControl1 : UserControl { public delegate void MyDelegate(object sender, EventArgs e);//建立委托 public MyDelegate myEven_click; public UserControl1() { InitializeComponent(); button1.Click += new EventHandler(button1_Click); //绑定委托事件 } private void button1_Click(object sender, EventArgs e) { if (myEven_click != null) { myEven_click(sender, e); } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 自定义控件的点击事件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { userControl11.myEven_click += new UserControls.UserControl1.MyDelegate(fuzhi);//把事件绑定到自定义的委托上 } public void fuzhi(object sender ,EventArgs e) { label1.Text = "456"; } } }
本文详细介绍了如何在C#中创建自定义控件,并使用委托和事件进行交互。通过实例展示了如何在用户控件(UserControl)中定义委托,然后在主窗体中订阅并响应这些事件。

被折叠的 条评论
为什么被折叠?



