1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace UserControls 11 { 12 public partial class UserControl1 : UserControl 13 { 14 public delegate void MyDelegate(object sender, EventArgs e);//建立委托 15 public MyDelegate myEven_click; 16 public UserControl1() 17 { 18 InitializeComponent(); 19 button1.Click += new EventHandler(button1_Click); //绑定委托事件 20 } 21 22 private void button1_Click(object sender, EventArgs e) 23 { 24 if (myEven_click != null) 25 { 26 myEven_click(sender, e); 27 } 28 } 29 } 30 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace 自定义控件的点击事件 11 { 12 public partial class Form1 : Form 13 { 14 public Form1() 15 { 16 InitializeComponent(); 17 } 18 19 private void Form1_Load(object sender, EventArgs e) 20 { 21 userControl11.myEven_click += new UserControls.UserControl1.MyDelegate(fuzhi);//把事件绑定到自定义的委托上 22 } 23 public void fuzhi(object sender ,EventArgs e) 24 { 25 label1.Text = "456"; 26 } 27 } 28 }
时间: 2024-12-20 04:43:45