就是窗体间传递变量的问题。比如form1有个txtbox。还有一按钮。点按钮,出现form2。form2里有个txtbox。往里面输入某个值,然后关闭form2,这个时候form1的txtbox里就出现刚才输入的那个值。我看了很多例子,都是在form2里 用 new form1,然后form1.show 来处理传值。但是我希望form1自始至终存在,而且只是当form2关闭后,form1的txtbox就出现刚才的输入。不知道该怎么做。
调试欢乐多
form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
form2:Form1 frm1;
public Form2(Form1 frm)
{
frm1 = frm;
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
frm1.textBox2.Text= textBox1.Text;
}
public Form2(Form1 frm)
{
frm1 = frm;
InitializeComponent();
}
原来还可以这样,学习了,留名备查.
static public string mem1;//静态变量
private void button1_Click(object sender, EventArgs e)
{
subForm form2 = new subForm();
form2.ShowDialog();
textBox1.Text = mem1;
}
子窗体:
private void button1_Click(object sender, EventArgs e)
{
mainForm.mem1 = textBox1.Text;
this.Close();
}
{
public Form1()
{
InitializeComponent();
} public string TextBox1Text
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
} private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show(this);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
Form1 frm1 = (Form1)this.Owner;
this.textBox2.Text = frm1.TextBox1Text;
}
}
这种方法 可以的 不过 实现不是LZ 想到的我修改了如下:Form1 窗体
public Form1()
{
InitializeComponent();
}
public string textBoxValue
{
set { this.textBox1.Text = value; }
get { return this.textBox1.Text; }
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Owner = this;
f2.Show();
}
Form2窗体:
public Form2()
{
InitializeComponent();
} private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 f1 = (Form1)this.Owner;
f1.textBoxValue = this.textBox1.Text;
}