您的位置:首页 > 其它

winform窗体间传值的问题

2007-08-07 16:49 411 查看
关于winform窗体间传值的解决方法已经有很多了,但我个人还是觉得通过中间对象的方法来实现好一些:

首先来定义一个中间对象Sender

using System;
using System.Text;
using System.Collections.Generic;
namespace demo
{
public class Observe
{
private string strText;
public string StrText
{
get
{
return strText;
}
set
{
strText = value;
}
}
public Observe()
{
// TODO: 在此处添加构造函数
}
}
}

然后在Form2(即发送值的窗体)中定义一个Observe成员

private Observe o_Observer;
public Observe observe
{
set
{
this.o_Observer = value;
}
}

在Form2的事件处理中改变o_Observe的StrText

private void button1_Click(object sender, System.EventArgs e)
{
o_Observer.StrText = this.textBox1.Text;
this.Close();
}

然后在Form1(即接收值的窗体中)取得Form2传来的值

private void button1_Click(object sender, EventArgs e)
{
Form2 fm= new Form2();
fm.observe = o_Observe;
fm.ShowDialog();
if (o_Observe.strText != null)
Textbox1.Text = o_Observe.strText;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: