我有两个winform form1和form2如下:
public partial class Form1 : Form
{
Form2 frm2;
int count = 0;
public bool fromForm2;
public Form1(bool fromForm2 = false)
{
InitializeComponent();
this.fromForm2 = fromForm2;
MessageBox.Show(fromForm2.ToString());
if (fromForm2 == true) {
test();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (frm2 == null)
{
frm2 = new Form2(); //Create form if not created
frm2.FormClosed += frm2_FormClosed; //Add eventhandler to cleanup after form closes
}
frm2.Show(this); //Show Form assigning this form as the forms owner
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2 = null; //If form is closed make sure reference is set to null
Show();
}
public void test ()
{
textBox1.Text = "ABCDFGHIJKLM";
MessageBox.Show(textBox1.Text);
}
}
表格2
public partial class Form2 : Form
{
Form1 f1;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objj = new Form1(true);
objj = (Form1)Application.OpenForms["Form1"];
objj.fromForm2 = true;
objj = null;
}
}
我想点击form2中的按钮,将运行test(),但我发现,如果我使用if(fromForm2 == true){} if语句,test()函数将不会更新(重绘?)要显示到textBox1的文本值,有谁知道这是什么原因?