实现点击Form1的send按钮则打开Form2,并将Form1文本框的值传给Form2。点击Form2的OK按钮则关闭Form2并将文本框的值传回Form1。
点击send打开Form2并传值有多种方式
委托传值
可以在new Form2时将值传给Form2,这时需要更改Form2的构造函数来接收,由于在Form2中不能调用Form1的控件,所以更改Form1的文本框的方法写在Form1里,用委托传给Form2,也是更改Form2的构造函数来实现。
代码如下:
namespace WindowsFormsApplication3
{
//定义全局的委托
public delegate void changeTextDelegate(string message);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//构造form2时将值和方法传给form2
Form2 form2 = new Form2(textBox1.Text,changeText);
form2.Show();
}
private void changeText(string message)
{
textBox1.Text = message;
}
}
}
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 重载Form2构造函数
/// </summary>
/// <param name="message">接收值</param>
/// <param name="changeText">委托接收方法</param>
public Form2(string message, changeTextDelegate changeText) : this()
{
textBox1.Text = message;
//将更改Form1文本框值的方法赋给Form2私有的委托
_changeText = changeText;
}
private changeTextDelegate _changeText;
private void button1_Click(object sender, EventArgs e)
{
_changeText(textBox1.Text);
this.Close();
}
}
}
事件传值
跟委托差不多
namespace WindowsFormsApplication3
{
//定义全局的委托
public delegate void changeTextDelegate(string message);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//构造form2时将值和方法传给form2
Form2 form2 = new Form2(textBox1.Text);
form2.changeTextEvent += changeText;
form2.Show();
}
private void changeText(string message)
{
textBox1.Text = message;
}
}
}
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string message) : this()
{
textBox1.Text = message;
}
public event changeTextDelegate changeTextEvent;
private void button1_Click(object sender, EventArgs e)
{
changeTextEvent(textBox1.Text);
this.Close();
}
}
}
用Owner属性
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(textBox1.Text);
form2.Show(this);
}
public void changeText(string message)
{
textBox1.Text = message;
}
}
}
namespace WindowsFormsApplication3
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string message) : this()
{
textBox1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.Owner;
form1.changeText(textBox1.Text);
//用fom1的Controls集合也可以
// form1.Controls["textBox1"].Text = textBox1.Text;
this.Close();
}
}
}
封装属性实时多次传值
上面的方法Form1只能传一次值给Form2.
用属性封装也能完成窗体传值,而且可以不关闭Form2,Form1多次传值给Form2,即可以实时传值。
把Form1的send按钮方法改成
private void button1_Click(object sender, EventArgs e)
{
if (form2 == null||form2.IsDisposed)
{
form2 = new Form2();
form2.textValue = textBox1.Text;
form2.Show(this);
}
else
{
form2.textValue = textBox1.Text;
}
}
在Form2里加属性
public string textValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
就可以了