public void ShowMyDialogBox() { Form2 testDialog = new Form2(); // Show testDialog as a modal dialog and determine if DialogResult = OK. if (testDialog.ShowDialog(this) == DialogResult.OK) { // Read the contents of testDialog's TextBox. this.txtResult.Text = testDialog.TextBox1.Text; } else { this.txtResult.Text = "Cancelled"; } testDialog.Dispose(); }
private void button1_Click(object sender, System.EventArgs e) { Form2 form = new Form2(); form.Show(); } }
Form2 fm2=new Form2();
fm2.show();要确保form2巳存在
private void button1_Click(object sender, System.EventArgs e)
{
Form2 myform = new Form2();
myform.Show();
}
{
Form2 testDialog = new Form2(); // Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
{
Form2 form = new Form2();
form.Show();
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 myform = new Form2();
myform.Show();
}
{
Form2 form = new Form2();
form.ShowDialog();
}
}
这种方法程序执行到form.ShowDialog();语句后,等待用户操作完成之后才执行下一条语句
而且,不会重复打开多个窗口。form2打开后,对FORM1的操作会失效。
{
if(this.frm2 != null)
{
this.frm2.Dispose();
}
frm2 = new Form2();
form.Show();
}这样不会无休止的打开Form2。 只能打开一个Form2窗口了。
{
Form2 myform = new Form2();
myform.Show();
}
myform.Show();