用委托实现窗体间传值
2010-04-21 14:07
281 查看
新建一个工程.在Form1中添加一个Label和一个Button.新建一个事件类,让它有一个string 类型的属性,用于传值.
代码
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace test
10 {
11 public partial class Form2 : Form
12 {
13 public event returnValueHandler returnValue;
14
15 public Form2()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 ReturnValueEventArgs ee = new ReturnValueEventArgs();
23 ee.strMessage = textBox1.Text;
24 returnValue(sender, ee);
25 }
26 }
27 }
在Form1中添加如下代码.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9
10 namespace test
11 {
12 public delegate void returnValueHandler (object sender ,ReturnValueEventArgs e);
13 public partial class Form1 : Form
14 {
15
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 Form2 frm = new Form2();
24 frm.Show();
25 frm.returnValue += new returnValueHandler(frm_returnValue);
26
27
28 }
29
30 void frm_returnValue(object sender, ReturnValueEventArgs e)
31 {
32
33 label1.Text = e.strMessage;
34 //throw new Exception("The method or operation is not implemented.");
35 }
36 }
37 }
这样就可以实现窗体之间通过委托传值.
代码
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace test
10 {
11 public partial class Form2 : Form
12 {
13 public event returnValueHandler returnValue;
14
15 public Form2()
16 {
17 InitializeComponent();
18 }
19
20 private void button1_Click(object sender, EventArgs e)
21 {
22 ReturnValueEventArgs ee = new ReturnValueEventArgs();
23 ee.strMessage = textBox1.Text;
24 returnValue(sender, ee);
25 }
26 }
27 }
在Form1中添加如下代码.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8
9
10 namespace test
11 {
12 public delegate void returnValueHandler (object sender ,ReturnValueEventArgs e);
13 public partial class Form1 : Form
14 {
15
16 public Form1()
17 {
18 InitializeComponent();
19 }
20
21 private void button1_Click(object sender, EventArgs e)
22 {
23 Form2 frm = new Form2();
24 frm.Show();
25 frm.returnValue += new returnValueHandler(frm_returnValue);
26
27
28 }
29
30 void frm_returnValue(object sender, ReturnValueEventArgs e)
31 {
32
33 label1.Text = e.strMessage;
34 //throw new Exception("The method or operation is not implemented.");
35 }
36 }
37 }
这样就可以实现窗体之间通过委托传值.
相关文章推荐
- 委托实现窗体间传值
- winform利用委托实现窗体间传值
- Winform利用委托实现子窗体传值给父窗体
- 02-使用委托与多线程实现窗体间传值
- C#Winform基础 应用委托传递函数实现不同窗体之间传值
- 转:c#委托事件实现窗体传值
- 【C#】使用委托实现窗体传值
- winform 利用委托实现窗体传值
- C# 利用委托实现窗体间传值
- C#的委托事件在winform窗体中实现传值备忘
- C#的委托事件在winform窗体中实现传值备忘
- c# winfrm 委托实现窗体相互传值
- 用委托实现窗体间传值
- 图解C#利用委托实现窗体传值
- c# winfrom 委托实现窗体相互传值
- 转:c#委托事件实现窗体传值通信
- 委托、事件--实现窗体传值!
- 使用委托实现窗体之间的传值--事件订阅实现传值
- C# 利用委托实现窗体间传值
- c# 用委托和事件实现不同窗体间的通讯