winfrom页面间传参
时间:2011-02-16 来源:马战鹏
如:现在有from1和from2两个窗体。想把from1中的一个文本值直接传给from2,让其显示:
你可以这样做一、重载from2的构造函数

1 public Form2(string a)
2 {
3 InitializeComponent();
4 this.textBox1.Text = a;
5 }
然后在from1的事件中写下这样的代码

1 string a = this.textBox1.Text;
2 Form2 frm = new Form2(a);//实例化from2的对象
3 //frm.Msg = a;
4 frm.ShowDialog();
这样就完成了。
二、为from2窗体增加属性
在from2窗体中写如下代码

/// <summary>然后在from1的事件中写如下代码
/// 为窗体增加属性
/// </summary>
private string msg;
public string Msg
{
get { return msg; }
set
{
msg = value;
this.textBox1.Text = msg;
}
}

string a = this.textBox1.Text;不知道还有没有其它方法,请大家多多指教
//Form2 frm = new Form2(a);//实例化from2的对象
frm.Msg = a;//为from2的属性赋值
frm.ShowDialog();
相关阅读 更多 +