我有以下代码:
in form1
private void button6_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form5 = new Form5();
Form Form6 = new Form6();
Form4.Show();
Form5.Show();
Form6.Show();
}
在 form5 中的我有一个必须关闭form4,form5和form6的按钮。如下:
private void button2_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form6 = new Form6();
Form4.Close();
Form6.Close();
this.Close();
}
但Form4
和Form6
仍在营业!
答案 0 :(得分:-1)
要以正确的方式关闭,您必须知道要关闭的内容。在您的情况下,您将在开始时创建新的表单,并在尝试销毁它们之前在本地可见其他名称。
Form Form6 = new Form6();两个点击按钮完全不同。然后,如果你想让它更明显,可以在构造函数方法中生成有趣的表单,并将定义作为类字段,如。
public partial class Form1 : Form
{
Form form2; // be sure all componentes see all forms
Form form3;
Form form4;
public Form1()
{
InitializeComponent();
form2 = new Form(); // create new Forms
form3 = new Form();
var button = new Button();
button.Click += new EventHandler(button_Click); // tell the button what should be called when click
form4 = new Form();
form4.Controls.Add(button); // add button progrimicaly to form
}
void button_Click(object sender, EventArgs e)
{
form2.Hide(); // hide on click
form3.Hide();
form4.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
form2.Show(); // show on load
form3.Show();
form4.Show();
}
}
答案 1 :(得分:-1)
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = (Form4) Application.OpenForms["Form4"];
Form5 form5 = (Form5) Application.OpenForms["Form5"];
form4.Close();
form5.Close();
}