1

I want to show a dialog (message box) before the actual form, and if user selects no, the application should completly be closed. I am trying to use the code below, but even after clicking on No the form will be shown!

public Form1()
{
    InitializeComponent();

    if (MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
        Application.Exit();
}

I also tried this.Clsoe but then I have an exepction on Application.Run()

What is the problem? Any idea what would be the best approach for doing so?

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • Make yourself a timer. Start the timer on your message box IF. Make the timer to fire after 1 second, execute Exit. When I do such things, I make myself an extra form which is only responsible to return the message box result. Then I open the desired form. What you try to do is Close it while it is loading. – st_stefanov May 15 '12 at 13:09
  • Why isn't the message box displayed before the form creation? – David Brabant May 15 '12 at 13:11
  • 1
    The best approach is to ask the user before opening the form. – stuartd May 15 '12 at 13:11
  • The constructor is called too early, before Application.Run() is called. So there's nothing to exit yet. Environment.Exit() always works. – Hans Passant May 15 '12 at 13:12

4 Answers4

5

How about putting it in your Program.cs (assuming you want to determine whether to launch the app or not)

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        if (
            MessageBox.Show(
                "Contiue or not", "Question", 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.None, 
                MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            Application.Run(new Form1());
    }
StaWho
  • 2,488
  • 17
  • 24
  • Actually, Application.Exit does nothing because there's no message pump active. This code works simply because it skips Application.Run. – digEmAll May 15 '12 at 13:20
1

Do that in program.cs, like this :

  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  if (MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
    Application.Run(new Form1());
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

Show your messagebox in the OnLoad event instead of the constructor e.g.:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    if (MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
    {
        Application.Exit(); // or this.Close();
    }
}

Application.Exit() doesn't work in the constructor because there isn't any form yet, so, no message pump to stop.
Also this.Close() raises an error because it causes the call of Dispose() on the form; immediately after Application.Run try to show up the form, but it is disposed and it throws an exception.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
1

Don't do things like that in a constructor. You should know whether you want to create an object before you create it. If you say you want to show the MessageBox before the actual form, then show it before you call the constructor, like before calling Application.Run().

Application.Exit() tries to terminate all message pumps, but they are not started yet, because Application.Run() starts them.
Also Application.Exit() closes all windows of the application, but there are none yet, because your Form1 isn't even constructed yet.
You're trying to exit the Application before it even has a chance to start running (Run hasn't been called yet).
So calling that method inside the application's only form's constructor doesn't make much sense.

Botz3000
  • 39,020
  • 8
  • 103
  • 127