c# winform 一个传递值的问题,很简单

来源:百度知道 编辑:UC知道 时间:2024/05/12 09:44:47
form1中有一个button1和一个textbox1,
在textbox1中输入"aaa",
点击后产生一个form2,form2有一个textbox2,自动将aaa传递给textbox2
这怎么完成??
请指教

您好,

您可以修改一下Form2的构造函数,例如:
Class Form2
{
public Form2(string msg)
{
this.textBox2.Text=msg;
}
}

然后在构造Form2的时候把这个“aaa”传递进去就OK了,例如:

private void Button1_Click(object sender, Eventargs e)
{
Form2 frm=new Form2(this.textBox1.Text);
frm.show();
}

这样就可以了。

Form2 frm=new Form2();
frm.Show();
frm.textBox2.Text =this.textbox1.Text;
//不是最好的方法

还可以这样.

public static string str;//变量申明区.

str = this.textbox1.Text;

textBox2.Text = Form2.str;

//也不是最好的办法.看你喜欢哪一种