关于委托的问题请教
程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ProcessMethodCall { public partial class Form1 : Form { private delegate void WriteTextBox(char ch); private WriteTextBox writeTextBox; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if(checkBox1.Checked == true) { groupBox2.Text = "运行中..."; groupBox2.Refresh(); textBox1.Clear(); textBox1.Refresh(); writeTextBox = new WriteTextBox(WriTextBox1); WriTxt(writeTextBox); groupBox2.Text = "任务1"; textBox3.Focus(); textBox3.SelectAll(); } if(checkBox2.Checked == true) { groupBox2.Refresh(); groupBox3.Text = "运行中..."; groupBox3.Refresh(); textBox2.Clear(); textBox2.Refresh(); writeTextBox = new WriteTextBox(WriTextBox2); WriTxt(writeTextBox); groupBox3.Text = "任务2"; textBox3.Focus(); textBox3.SelectAll(); } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { } private void WriTxt(WriteTextBox wMethod) { string strdata = textBox3.Text; for(int i = 0; i < strdata.Length; i++) { wMethod(strdata[i]); DateTime now = DateTime.Now; while(now.AddSeconds(1) > DateTime.Now) { } } } private void WriTextBox1(char ch) { textBox1.AppendText(ch + "\r"); } private void WriTextBox2(char ch) { textBox2.AppendText(ch + "\r"); } private void groupBox3_Enter(object sender, EventArgs e) { } } }
我理解的工作流程是否正确?
1.定义一个委托WriteTextBox,这个委托没有返回值,接受char类型参数。
2.创建一个writeTextBox委托实例。
3.WriTxt方法将WriteTextBox委托作为参数使用,
这个方法把textBox3中的得到的字符串以单个字符的形式填充委托的参数。
4.委托调用WriTextBox1方法,把从WriTxt方法中的得到的字符串写入textBox1。
有两个问题:
1.按照流程应该是先获取字符,再把字符写入到textBox1里,为什么
writeTextBox = new WriteTextBox(WriTextBox1);
在
WriTxt(writeTextBox);
前面执行?
2. private void groupBox3_Enter(object sender, EventArgs e)
{
}
这句话没有任何用处,为什么删了会报错?怎么才能正常删除?