C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP ME! CODING ERROR! Posted by rajdeep_net on 31 Mar 2009 at 12:27 PM
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 :)

Report
Re: HELP ME! CODING ERROR! Posted by nebby555 on 1 Nov 2009 at 2:50 PM
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);
}
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.