关闭一个表单,然后调用另一个表单 [英] Closing a form and then call another one

查看:28
本文介绍了关闭一个表单,然后调用另一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想关闭当前的表单 (MainForm),然后打开第二个表单 (Form).

I want to close the current form I'm on (MainForm) and then opening a second one (Form).

我试过了:

private void buttonStartQuiz_Click(object sender, EventArgs e)
{
    this.Close();

    Form2 form2 = new Form2();
    form2.ShowDialog();
}

或者在 form2.ShowDialog() 之后添加 this.Close(); 也不起作用.

Or adding the this.Close(); after form2.ShowDialog() also doesn't work.

有什么提示吗?

不妨通过在 form2.ShowDialog() 之后添加 this.Close() 来添加它,它仅在我关闭新表单时关闭.如果我选择 form2.Show() 而不是它立即关闭两个表单.

Might as well add that by adding this.Close() after form2.ShowDialog() it close only when I close the new form. If I choose form2.Show() instead it immediately closes both of the forms.

推荐答案

更改

this.Close();

致:

this.Hide();

因为您无法关闭主应用程序窗口并希望应用程序在其后运行.您必须隐藏主窗体或将主窗口更改为仍然打开的窗口.

Because you can't Close Main Application window and want to application runs after it. You must hide main form or change main window to window who was still opened.

在这种情况下,您必须在 ShowDialog() 结束后关闭主窗口.那么你必须在这个按钮事件函数的末尾添加this.Close()

In this case you must close main window after ShowDialog() was ended. Then you must add on the end of this button event function this.Close()

您的代码新代码是:

private void buttonStartQuiz_Click(object sender, EventArgs e)
    {
        // hide main form
        this.Hide();

        // show other form
        Form2 form2 = new Form2();
        form2.ShowDialog();

        // close application
        this.Close();
    }

这篇关于关闭一个表单,然后调用另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆