用委托实现窗体间传值
1.新建一個(gè)工程.在Form1中添加一個(gè)Label和一個(gè)Button.新建一個(gè)事件類(lèi),讓它有一個(gè)string 類(lèi)型的屬性,用于傳值.
1?///ReturnValueEventArgs.cs
?2?using?System;
?3?using?System.Collections.Generic;
?4?using?System.Text;
?5?
?6?namespace?test
?7?{
?8?????public??class?ReturnValueEventArgs:EventArgs?
?9?????{
10?????????private?string?str;
11?
12?????????public?string?strMessage
13?????????{
14?????????????get
15?????????????{
16?????????????????return?str;
17?????????????}
18?????????????set
19?????????????{
20?????????????????str?=?value;
21?????????????}
22?????????}
23?????}
24?}
25?
2.新建一個(gè)窗體Form2.添加一個(gè)TextBox和一個(gè)Button.添加如下代碼.
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?}
3.在Form1中添加如下代碼.
?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?}
轉(zhuǎn)載于:https://www.cnblogs.com/kjun/archive/2010/08/22/1805846.html
總結(jié)
以上是生活随笔為你收集整理的用委托实现窗体间传值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android模拟qq登陆,模拟qq登录
- 下一篇: t-sql中的事务控制及错误处理