关于winform窗体间传值的解决方法已经有很多了,但我个人还是觉得通过中间对象的方法来实现好一些:
首先来定义一个中间对象Sender
然后在Form2(即发送值的窗体)中定义一个Observe成员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: 在此处添加构造函数
}
}
}
private
Observe o_Observer;
public Observe observe
{
set
{
this .o_Observer = value;
}
}
public Observe observe
{
set
{
this .o_Observer = value;
}
}
private
void
button1_Click(
object
sender, System.EventArgs e)
{
o_Observer.StrText = this .textBox1.Text;
this .Close();
}
{
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;
}
{
Form2 fm = new Form2();
fm.observe = o_Observe;
fm.ShowDialog();
if (o_Observe.strText != null )
Textbox1.Text = o_Observe.strText;
}