设为首页 加入收藏

TOP

C# 委托基础1.0
2019-09-17 18:57:15 】 浏览:37
Tags:委托 基础 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小程序,探讨了一下委托。学习、理解委托是学习多线程的基础

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇DS控件库 在Combobox中嵌入远程桌.. 下一篇C# 数组Array

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目