c#自定義控件中的事件處理


 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM