elvispt
elvispt

Reputation: 4942

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.

Upvotes: 17

Views: 101574

Answers (12)

Frederick Lowe
Frederick Lowe

Reputation: 1

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
        Form1 f1 = new Form1();
        f1.Show();
    }

Is all you need

Upvotes: 0

Essam Fahmy
Essam Fahmy

Reputation: 2255

Just replace this.Close() with this.Hide(), it will work perfectly!

Upvotes: 0

Justin
Justin

Reputation: 11

This works fine for me...

private void button_Name_Click(object sender, EventArgs e)
    {
        form_2 Form2 = new Form2();
        Form2.ShowDialog();
        this.Close();
    }

Upvotes: 1

Lâm Ánh
Lâm Ánh

Reputation: 51

Try this:

btCancel.FindForm().Close();

btcancel is a button that lays on the form you want to close.

Upvotes: -1

tharaka077
tharaka077

Reputation: 1

new play().Show();
this.Dispose();

Upvotes: -2

Akilan
Akilan

Reputation: 9

Put this in main form

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

Worked for me.

Upvotes: -1

mahmoud_waheed
mahmoud_waheed

Reputation: 21

this.Visible = false;
Form2 form2 = new Form2();
form2 .ShowDialog();
this.Close();

Upvotes: 0

rrkbca86
rrkbca86

Reputation: 1

Just enable or disable the form itself, instead of trying to close.

Code it like:

this.enabled = false;

in the click event or wherever you want it.

Hope it helps.

Upvotes: 0

nicOL
nicOL

Reputation: 1

Form2 frm2 = new Form2();
frm2.ShowDialog();

and

this.Close();

is not a good combination..

frm2 will actually load but you can't even see it because this.Close(); will close every form just in a millisecond...

this.Hide();

works...

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941455

You can mess with ApplicationContext but the .NET framework already has very good support for this windowing mode with the WindowsFormsApplicationBase class. Its ShutdownStyle property is available to let the program shut down only after the last window was closed. Make the code in Program.cs look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;  // Add reference to Microsoft.VisualBasic!!

namespace WindowsFormsApplication1 {
  class Program : WindowsFormsApplicationBase {
    [STAThread]
    static void Main(string[] args) {
      var app = new Program();
      app.EnableVisualStyles = true;
      app.ShutdownStyle = ShutdownMode.AfterAllFormsClose;
      app.MainForm = new Form1();
      app.Run(args);
    }
  }
}

Upvotes: 6

smartali89
smartali89

Reputation: 209

You are closing the form first, you must load the 2nd form first.

private void buttonStartQuiz_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
this.Close();
}

Upvotes: -1

Svisstack
Svisstack

Reputation: 16616

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

Upvotes: 36

Related Questions