Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

c# - Closing a form and then call another one

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

I've tried:

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

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

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

Any hints?

EDIT: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change

this.Close();

To:

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.

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()

Your code new code is:

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();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...