Gulam
Gulam

Reputation: 73

Creating a confirmation box c#

I am having a bit of a problem. I am still new to C# however I am progressing slowly and learning new things.

However I am confused. I am trying to create a confirmation box. However it doesn't seem to function the way it is intended to.

Here is the code:

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    MessageBox.Show("Are you sure you want to exit off the application", "Are you sure?", MessageBoxButtons.YesNoCancel); //Gets users input by showing the message box

    if (DialogResult == DialogResult.Yes) //Creates the yes function
    {
        this.Close(); //Exits off the application
    }

    else if (DialogResult == DialogResult.No)
    {
        //Does nothing
    }

Upvotes: 3

Views: 7961

Answers (2)

Ahmed Mustafa
Ahmed Mustafa

Reputation: 111

Guess this is what you want,

DialogResult result1 = MessageBox.Show("Is Dot Net Perls awesome?",
                                       "Important Question",
                                       MessageBoxButtons.YesNo);

For more details check this: https://www.dotnetperls.com/messagebox-show

Upvotes: 3

David
David

Reputation: 218847

You're not capturing the result of the dialog. I'm surprised this would even compile with those if statements. (And if it doesn't compile then you really missed an important detail of the problem. Compiler errors are worth paying attention to.)

You need to capture the result:

var result = MessageBox.Show(...);
if (result == DialogResult.Yes)
{
    this.Close();
}
//...

Upvotes: 7

Related Questions