0

I'm doing an app in c# and I'm having a little problem while trying to close my app.

So, when my app starts, there's a message box that appears on the load. Then I have 3 options, yes no and cancel.

I'd like that, if the user click on the exit button of that messageBox, that it closes the whole app. But instead, it just closes the message box and shows the next of my app.

I've tried things like Environment.Exit(0); Application.Exit... but nothing worked

Here's my code !

private void Form1_Load(object sender, EventArgs e)
    {
       //this is executed on the load of my form, the user has to choose a difficulty that will change the depth

        // IMPORTANT : 
        //Facile stands for Yes
        //Moyen stands for No
        //Difficile stands for Cancel
        MessageBoxManager.Yes = "Facile";
        MessageBoxManager.No = "Moyen";
        MessageBoxManager.Cancel = "Difficile";
        MessageBoxManager.Register();
        DialogResult result = MessageBox.Show("Quelle est la difficulté?", "Niveau de jeu",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

            //according to the chosen button, the value of depth will change
            if (result == DialogResult.Yes) // facile
            {
                profondeur = 3;

            }
            else if (result == DialogResult.No) 
            {
                profondeur =6 ;
            }
            else if (result == DialogResult.Cancel) 
            {
                profondeur = 9;
            }
        else {

            Environment.Exit(0); //if any other button is clicked ?

        }

    }

Thank you in advance !

David Silva
  • 67
  • 1
  • 11
  • Are you *sure* your code is taking the path you think? Have you tried debugging and stepping through it? – rory.ap Jun 01 '18 at 20:16
  • 1
    Use Application.Exit(). https://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application – Dan Hunex Jun 01 '18 at 20:18
  • 3
    The dialog result code for the Close button in a Yes/No/Cancel dialog is Cancel. Easy to find out with the debugger btw, be sure to practice using it. You want to at least know how to set a breakpoint and single-step the code. – Hans Passant Jun 01 '18 at 20:19
  • So you're telling me that the Cancel button and the close button do the same thing ? – David Silva Jun 01 '18 at 20:21
  • Yes. See [this answer](https://stackoverflow.com/questions/1546082/yesno-messagebox-not-closing-when-x-button-clicked#2543941) – stuartd Jun 01 '18 at 20:23
  • @HansPassant You are right ! But now I have a question, I need 3 buttons in that message box, 2 arent enough, how can I have 3 buttons but one of dont being Cancel ? – David Silva Jun 01 '18 at 20:25
  • 2
    You have 3 buttons, Yes, No and Cancel. You can't get more. When the user clicks Cancel then it is not inconceivable that terminating the program makes sense. If you need Cancel to do something else then you cannot implement your feature with MessageBox. Not exactly a show-stopper, it is not hard to replace with a Form. – Hans Passant Jun 01 '18 at 20:31
  • 1
    Ok, thank you, it's much clear to me now ! Cheers – David Silva Jun 01 '18 at 20:35

0 Answers0