单击即可关闭多个窗口

时间:2014-08-05 20:44:17

标签: c#

我想同时关闭两个表单。我有一个以程序开头的主要表单。当用户点击按钮时,主窗体将隐藏,其他窗体将弹出。在第二个表单上,如果用户单击“返回主”按钮,它应隐藏第二个表单并显示主表单。但问题是如果用户试图关闭第二个表单,它也应该关闭主表单。我怎样才能关闭主表格

6 个答案:

答案 0 :(得分:3)

我只是将Application.Exit()用于此线程请求的内容。

Application.Exit();

更新:更正

我曾经说过这不会调用表格结束事件,但在文档中它确实在这里调用它是文档的链接

http://msdn.microsoft.com/en-us/library/ms157894(v=vs.110).aspx

答案 1 :(得分:2)

如果您指定了为回到主表单而编写的代码,那就更好了,所以我可以通过更改您的代码来帮助您。但现在因为我不知道你是怎么做到的,我必须为这两个任务编写代码。 可以使用布尔变量来执行您想要的操作。请遵循以下代码:

public partial class MainForm : Form
{
//"Click" event of the button that should opens the second form:
private void goToSecondForm_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(); //Or you can write it out of this method.
        this.Hide(); //Hides the main form.
        f2.ShowDialog(); // Shows the second form.
        this.Show(); // Shows the main form again, after closing the second form using your own button.
    }
}


public partial class Form2 : Form
{
    bool selfClose = false; //False shows that user closed the second form by default button and true shows that user closed it by your own button.
    //"Click" event of the button that should closes just the second form and returns user to the main form:
    private void ownCloseButton_Click(object sender, EventArgs e)
    {
        selfClose = true; //Means user clicked on your own button.
        this.Close(); //So the program closes the second form and runs f2_FormClosed method, but because selfClose became true here, happened nothing there and program will go back to goToSecondForm_Click method in the main form and will run this.Show() .
    }
//"FormClosed" event of the second form :
//Whether user clicked on your own button or on the default one, this method will run.
private void f2_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (!selfClose) //It means user didn't click on your own button and both of forms must be closed .
            Application.Exit(); //So the program closes all of forms (actually closes the program) and couldn't access to any other commands (including this.Show() in goToSecondForm_Click method).
    }
}

答案 2 :(得分:0)

正如其他人所说,当您的子表单关闭时,您需要以某种方式在主表单上调用.Close()。但是,正如您所指出的那样,您的子表单中没有自动引用主表单!这给你留下了一些选择。

<强> 1。立即退出应用程序。 这是通过在子表单的“返回主”按钮的单击事件处理程序中调用Application.Exit();来完成的。它将立即关闭所有形式,这可能只是你想要的。

// .. ChildForm code ..
void OnBackToMainClicked(object sender, EventArgs e)
{
    Application.Exit();
}

<强> 2。将对主窗体的引用传递给子窗体。 这可能是解决这个问题的最常用方法。在主表单中创建子表单时,您需要按如下方式传递引用:

// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
    var childForm = new ChildForm(this);
    childForm.Show();
}

// .. ChildForm code ..
private MainForm mainForm; // This is where the child form will keep a reference to
                           // the main form that you can use later

public ChildForm(MainForm mainForm)
{
    // This is the child form's constructor that we called above,
    // and it's where we'll save the reference to the main form
    this.mainForm = mainForm;
}

// This also needs to be the event handler for the close event
void OnBackToMainClicked(object sender, EventArgs e)
{
    this.Close();
    mainForm.Close();
}

第3。在子表单的FormClosed事件上添加事件处理程序。如果您担心将主应用程序逻辑保持在主窗体的控制之下,这是一种解决问题的安全方法。它类似于上面Lamloumi建议的解决方案,但它都是在主窗体的代码中完成的。

// .. MainForm code ..
void OnGoToChildForm(object sender, EventArgs e)
{
    var childForm = new ChildForm(this);
    childForm.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
    childForm.Show();
}

void SecondForm_FormClosed(object sender, EventArgs e)
{
    // Perform any final cleanup logic here.
    this.Close();
}

答案 3 :(得分:-1)

通常情况下,在Home表单中你会有这样的话:

  SecondForm second= new SecondForm ();
       second.Show();
       this.Hide();

在SecondForm中你必须像这样关闭事件:

   public class SecondForm :Form{

            public SecondForm()
                  {
                 InitializeComponent();
                 this.FormClosed += new FormClosedEventHandler(SecondForm_FormClosed);
                   }

                void SecondForm_FormClosed(object sender, FormClosedEventArgs e)
                {
                Application.Exit();
                 }
}

确保在关闭表单后关闭应用程序。因为如果不这样做,Home from仍然是活动的并且隐藏起来。

答案 4 :(得分:-1)

Form1 _FirstForm = New Form1();
Form2 _SecondForm = New Form2();
MainForm _MainForm = new MainForm();

_FirstForm.Close();
_SecondForm.Close();
_MainForm.Show();

答案 5 :(得分:-1)

在Form2中使用它

private void button1_Click(object sender, EventArgs e)
        {
            Form1.FromHandle(this.Handle);
        }

现在Handle是一样的; 希望这项工作。