16

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.

Mike G
  • 4,232
  • 9
  • 40
  • 66
elvispt
  • 4,832
  • 7
  • 27
  • 35

12 Answers12

36

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();
    }
Svisstack
  • 16,203
  • 6
  • 66
  • 100
  • But I do want to close it. Although I guess that I could hide it, then when exiting form2 I could call it back? – elvispt May 01 '10 at 18:40
  • @elvispt: Yes, you can do that. Change this.Close(); to this.Show(); – Svisstack May 01 '10 at 18:42
  • What would be the correct way to call the hidden (main) from form2? – elvispt May 01 '10 at 18:48
  • 1
    @elvispt: You can add to Form2() constructor, argument of type Form. You with this argument passing main window (this). And if you want from Form2 you can call this main window with Show funciton. You may change with this option ShowDialog() to Show() because hard to show form when he is blocked with ShowDialog() method. – Svisstack May 01 '10 at 18:51
  • I'm sorry I don't understand. What am I supposed to do in the constructor? public Form2(Form a) ? How could I call the hidden form, let's say on the Form2_FormClosed event? – elvispt May 01 '10 at 19:07
  • 1
    elvispt: I suggest going through some OOP tutorials (preferably in C#), because you clearly don't have a clue what he said :( – mnn May 01 '10 at 19:37
  • I don't understand the point of doing this: public Form2(Form form). – elvispt May 01 '10 at 19:47
  • I managed to get it work. Although understand why is the problem. – elvispt May 01 '10 at 19:53
6

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);
    }
  }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

This works fine for me...

private void button_Name_Click(object sender, EventArgs e)
    {
        form_2 Form2 = new Form2();
        Form2.ShowDialog();
        this.Close();
    }
Justin
  • 11
  • 1
  • While this may answer the question, it is better to explain the essential parts of the answer and possibly what was the problem with OPs code. – pirho Dec 10 '17 at 21:09
0
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...

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
nicOL
  • 1
  • 1
0

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.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
0
this.Visible = false;
Form2 form2 = new Form2();
form2 .ShowDialog();
this.Close();
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – dbugger May 02 '14 at 19:48
  • but this is the answer for his question please try it before saying it is not working his question was I want to close the current form I'm on (MainForm) and then opening a second one (Form). and this is the answer for it – mahmoud_waheed May 05 '14 at 09:46
  • Mainly, you just need to tell what your code does, rather than just putting out lines of code. – Cullub Sep 02 '14 at 18:59
0

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

Essam Fahmi
  • 1,920
  • 24
  • 31
0
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
        Form1 f1 = new Form1();
        f1.Show();
    }

Is all you need

-1

Try this:

btCancel.FindForm().Close();

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Lâm Ánh
  • 51
  • 7
-1

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();
}
smartali89
  • 209
  • 2
  • 8
-1

Put this in main form

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

Worked for me.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Akilan
  • 9
-2
new play().Show();
this.Dispose();
Pang
  • 9,564
  • 146
  • 81
  • 122