先贴个委托类
1 class ProcessDelegate
2 {
3 public delegate double processDele(double param1, double param2);
4 public static double Multiply(double param1, double param2)
5 {
6 return param1 * param2;
7 }
8 public static double Divide(double param1, double param2)
9 {
10 return param1 / param2;
11 }
12 }
2 {
3 public delegate double processDele(double param1, double param2);
4 public static double Multiply(double param1, double param2)
5 {
6 return param1 * param2;
7 }
8 public static double Divide(double param1, double param2)
9 {
10 return param1 / param2;
11 }
12 }
然后是窗体类的设计
1 namespace 委托展示乘除法
2 {
3 public partial class Form1 : Form
4 {
5 public Form1()
6 {
7 InitializeComponent();
8 }
9
10 private ProcessDelegate.processDele process;
11
12 private void button1_Click(object sender, EventArgs e)
13 {
14 /*processDele process;*/
15
16 double p1 = Convert.ToDouble(textBox1.Text);
17 double p2 = Convert.ToDouble(textBox2.Text);
18
19 double r = 0;
20
21 r = process(p1, p2);
22
23 label1.Text = Convert.ToString(r);
24 }
25
26 private void radioButton2_CheckedChanged(object sender, EventArgs e)
27 {
28 process = null;
29 process = new ProcessDelegate.processDele(ProcessDelegate.Divide);
30 }
31
32 private void radioButton1_CheckedChanged(object sender, EventArgs e)
33 {
34 process = null;
35 process = new ProcessDelegate.processDele(ProcessDelegate.Multiply);
36 }
37
38 private void Form1_Load(object sender, EventArgs e)
39 {
40 process = new ProcessDelegate.processDele(ProcessDelegate.Multiply);
41 }
42 }
43 }
2 {
3 public partial class Form1 : Form
4 {
5 public Form1()
6 {
7 InitializeComponent();
8 }
9
10 private ProcessDelegate.processDele process;
11
12 private void button1_Click(object sender, EventArgs e)
13 {
14 /*processDele process;*/
15
16 double p1 = Convert.ToDouble(textBox1.Text);
17 double p2 = Convert.ToDouble(textBox2.Text);
18
19 double r = 0;
20
21 r = process(p1, p2);
22
23 label1.Text = Convert.ToString(r);
24 }
25
26 private void radioButton2_CheckedChanged(object sender, EventArgs e)
27 {
28 process = null;
29 process = new ProcessDelegate.processDele(ProcessDelegate.Divide);
30 }
31
32 private void radioButton1_CheckedChanged(object sender, EventArgs e)
33 {
34 process = null;
35 process = new ProcessDelegate.processDele(ProcessDelegate.Multiply);
36 }
37
38 private void Form1_Load(object sender, EventArgs e)
39 {
40 process = new ProcessDelegate.processDele(ProcessDelegate.Multiply);
41 }
42 }
43 }