Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

HELP ME! CODING ERROR!

rajdeep_netrajdeep_net Posts: 4Member
Hello There!

I am a newbie in C# programming. I am working on a simple Windows form in VS.NET 2005, where there is a label displaying "Welcome to DialogResult Testing form". I have a Exit Button. I want that as soon as the user clicks on the Button in the form, the code in
the Button1_Click Event should get executed which will display a confirmation MessageBox. The code I have entered for the Click Event is something like this:

//CODE

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Are you Sure to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (MessageBox.Show"Are you Sure to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)

{
Application.Exit();
}
else
{
MessageBox.Show("Thanx for not terminating me!", "As you wish",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

//CODE ENDS//


Everything went fine, but suddenly a Major problem aroused! On clicking the Exit Button in the form, I get a Confirmation MessageBox displaying, "Are you Sure to Exit. Click Yes to Confirm and No to continue". Below there are two buttons, Yes and No, as per as my coding. But then, when I click on Yes Button to exit the Application, the MessageBox pops up again asking for the same confirmation. Again I click on Yes Button on the MessageBox and then, finally the application gets terminated. The same is in the case of the No button on the MessageBox. I click on No to continue but then the same MessageBox asking for confirmation pops up again. I again click on No and then get the Message "Thanx for not terminating me".

The problem here is that, when I run the application and click on Exit Button, a MessageBox is displayed asking for confirmation. But, if I click on Yes to exit the application, the same confirmation MessageBox is displayed again. And then when I click on Yes Button again, does exits the application. The same is with the No button.

Why does the MessageBox Pops up twice, instead I programmed it for just executing the code after recieving the input from the user, only once. Please help me out. Its very important. If possible, write me the correct code for this. Sorry if I wrote too long.

A friend in need :( is a friend indeed :)

Comments

  • nebby555nebby555 Posts: 1Member
    Just simply remove the first boxes you entered, the IF statement alone is sufficient for exit box, see below
    //CODE

    private void button1_Click(object sender, EventArgs e)
    {

    if (MessageBox.Show"Are you Sure to Exit. Click Yes to Confirm and No to continue", "WinForm", MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)

    {
    Application.Exit();
    }
    else
    {
    MessageBox.Show("Thanx for not terminating me!", "As you wish",MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    }


Sign In or Register to comment.