如何同时关闭两个表格? [英] How to close two forms at the same time?
问题描述
我有3种形式: Form1 , Form2 , Form3 。
当您在 Form1
上单击 buttonPlay
时, Form2
pops起来。当您点击 Form2
上的 button1
时, Form1
和 Form2
应该关闭, Form3
会弹出。
这是我在按钮1 的点击事件 Form2
上的代码:
I have 3 forms: Form1, Form2, Form3.
When you click on buttonPlay
on Form1
, Form2
pops up. When you click on button1
on Form2
, both Form1
and Form2
are supposed to close and Form3
will pop up.
This is my code on Form2
on the Click event of button1
:
private void button1_Click(object sender, EventArgs e)
{
Form1 x = new Form1();
//SUPPOSEDLY hides Form1
x.Hide();
//Hides Form2
this.Hide();
Form3 f3 = new Form3();
//Shows Form3
f3.Show();
}
我试过关闭()
和 Dispose()
并使用< identifier> .Visible = false;
并且没有人工作。
只关闭 Form2
,但我需要两者 Form1
& Form2
消失。我试图寻求一些帮助,但我们无法修复它。
谢谢!
I have tried Close()
and Dispose()
and using <identifier>.Visible = false;
and none worked.
It only closes Form2
, but I need both Form1
& Form2
to disappear. I have tried to ask for some help but we weren't able to fix it.
Thank you!
推荐答案
让代码告诉你如何使其工作:
Let codes tell you how to make this worked:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
}
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f)
{
this.f1 = f;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f1.Hide();
this.Hide();
new Form3(f1).Show();
}
}
public partial class Form3 : Form
{
Form1 f1;
public Form3(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f1.Close();
}
}
这篇关于如何同时关闭两个表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!