|
[.NET-Winform]winform--class--winform之间传值 |
麦林 发表于 2007/8/17 14:05:49 | 类:public static string str="";
WinForm a类.str = textbox.Text;
WinForm bstring str = 类.str;
中间类:using System;namespace WindowsApplication1{ //定义一个委托 public delegate void SendMessage(string s); public class cResult { //声明一个类事件,类型是上面的委托 public event SendMessage txtChange; //存放结果的中间变量 public string r1 = string.Empty ; /*定义一函数,目的是在form2中调用次函数,然后引发txtChange事件 而txtChange事件具体实现在form1中,正好可以改变文本框text值*/ public void ChangeText(string s ){ //判断是否为空 if (txtChange!=null) { //触发事件 txtChange(s); } } }} 主窗体:private void button1_Click(object sender, System.EventArgs e) { //实例化一个中间类 cResult r = new cResult(); //订阅此类的txtChange事件为SendMessage,具体实现内容在方法:Message中 r.txtChange +=new SendMessage(Message); //实例化form2 Form2 fr2 = new Form2(r); //显示form2窗体.fr2.Show()两个窗体都可以看,但是ShowDialog方法,最小化其中一个,另外一个也最小化 fr2.ShowDialog(); }private void Message(string s) { //form2的文本框值给form1的文本框 this.textBox1.Text=s; } 副窗体: //定义一个中间类 private cResult r; //构造函数 public Form2(cResult r):this() { //接收从form1传过来的中间类cResult this.r=r; }private void textBox1_TextChanged(object sender, System.EventArgs e) { //当文本框改变时,调用中间类的ChangeText函数,从而引发txtChange事件 r.ChangeText(this.textBox1.Text ); }
|
|
|
|

|
.: 公告
|
|
| « | November 2025 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | | | |
|
.: 我的分类(专题)
|
|

.: 最新日志
.: 最新回复
|
|

blog名称:栗色?蓝色? 日志总数:449 评论数量:201 留言数量:37 访问次数:2271836 建立时间:2006年5月16日 |
|

.: 留言板
|

.: 链接
|

|