代碼如下:
1private ProcessNumber pn = null;
2ShowNumberDel del = null;
3
4private void Form1_Load(object sender, EventArgs e)
5{
6 pn = new ProcessNumber(100);
7}
8
9//到控制台
10private void ShowInConsole(object[] items)
11{
12 foreach (object item in items)
13 {
14 Console.WriteLine(item);
15 }
16}
17
18//到ListBox
19private void ShowInListBox(object[] items)
20{
21 listBox1.Items.Clear();
22 foreach (object item in items)
23 {
24 listBox1.Items.Add(item);
25 }
26}
27
28//到文本文件
29private void ShowInFile(object[] items)
30{
31 using (StreamWriter sw = new StreamWriter("Test.txt", true))
32 {
33 foreach (object item in items)
34 {
35 sw.WriteLine(item);
36 }
37 }
38}
使用委托:
1private void button1_Click(object sender, EventArgs e)
2{
3 pn.ProcessItems(new ShowNumberDel(ShowInConsole));
4}
5
6private void button2_Click(object sender, EventArgs e)
7{
8 pn.ProcessItems(new ShowNumberDel(ShowInListBox));
9}
10
11private void button3_Click(object sender, EventArgs e)
12{
13 pn.ProcessItems(new ShowNumberDel(ShowInFile));
14}
15
16private void button4_Click(object sender, EventArgs e)
17{
18 del += new ShowNumberDel(this.ShowInListBox);
19 del += new ShowNumberDel(this.ShowInFile);
20
21 pn.ProcessItems(del);
22}