C# 委托基础1.0

在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小程序,探讨了一下委托。学习、理解委托是学习多线程的基础

猜你喜欢

转载自www.cnblogs.com/xiongmujiang/p/10716883.html