Dumbo
Dumbo

Reputation: 14122

Exiting application on MessageBox call in constructor

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?

Upvotes: 1

Views: 13671

Answers (4)

StaWho
StaWho

Reputation: 2488

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

Upvotes: 5

digEmAll
digEmAll

Reputation: 57220

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.

Upvotes: 1

Botz3000
Botz3000

Reputation: 39620

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.

Upvotes: 1

Antonio Bakula
Antonio Bakula

Reputation: 20693

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

Upvotes: 1

Related Questions