在c# 1.0中提出了一种新特性叫作:委托。委托本质上一种类型。是对特定方法的抽象,定义委托后,可以将方法封装,把方法当参数,传递

 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 windowsformstest
11 {
12     public partial class form1 : form
13     {
14 
15         delegate void mydelget(richtextbox gg);
16        
17 
18         public form1()
19         {
20             initializecomponent();
21         }
22 
23         private void button1_click(object sender, eventargs e)
24         {
25             mydelget my = new mydelget(this.writetextbox1);
26             if (checkbox1.checked == true)
27             {
28                 richtextbox1.clear();
29                 richtextbox1.refresh();
30                 //this.writetextbox1();
31                 my(richtextbox1);
32                 richtextbox1.focus();
33                 richtextbox1.selectall();
34             }
35             if (checkbox2.checked == true)
36             {
37                 richtextbox2.clear();
38                 richtextbox2.refresh();
39                 //this.writetextbox2();
40                 my(richtextbox2);
41                 richtextbox2.focus();
42                 richtextbox2.selectall();
43             }
44         }
45 
46         public void writetextbox1(richtextbox rich)
47         {
48             string data = textbox1.text;
49             for (int i = 0; i < data.length; i++)
50             {
51                 rich.appendtext(data[i].tostring());
52                 //延时
53                 datetime now = datetime.now;
54                 while (now.addseconds(1) > datetime.now)
55                 {                 
56                 }
57             }
58         }
59 
60         //public void writetextbox2()
61         //{
62         //    string data = textbox1.text;
63         //    for (int i = 0; i < data.length; i++)
64         //    {
65         //        richtextbox2.appendtext(data[i].tostring());
66         //        //延时
67         //        datetime now = datetime.now;
68         //        while (now.addseconds(1) > datetime.now)
69         //        {
70 
71         //        }
72         //    }
73 
74         //}
75     }
76 }

运行结果:

通过winform小程序,探讨了一下委托。学习、理解委托是学习多线程的基础