Why when choosing No it's asking twice? - c#

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to exist ?",
"Are you sure you want to exist ?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.No)
{
e.Cancel = true;
Application.Exit();
}
}
If i click Yes it's existing. But if i click No the message is show again and only on the second time the No take effect.
And i have a backgroundworker and a richTextView that i update. e.Cancel = true and application.exit is enough ?

Your logic is not right, you need to do one of those actions and not both depending on what was picked.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to exit ?",
"Are you sure you want to exit ?",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.No)
{
e.Cancel = true; // cancels the close action
}
else
Application.Exit(); // closes the app, this might not be necessary. Just proceeding with the form close is enough UNLESS this is not the main application form
}

If you choose "No", you cancel the current FormClosing event with e.Cancel = true;.
But then you call Application.Exit(); anyway. So the Form gets closed again and FormClosing is raised another time.
Remove the line Application.Exit() and everything should work fine.

Related

How can I pervent a regular user from closing my GUI, in C#? [duplicate]

I have a C# GUI application. When the user clicks on the red 'X' (for closing the app) I want to show a message and ask if he really wants to close it.
I found a solution:
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}else if (dialog == DialogResult.No)
{
//don't do anything
}
When the user clicks 'yes', the application should terminate completely. (Is Application.Exit() correct for this purpose?)
When the user clicks 'no', the DialogResult/MessageBox should close, but the application should stay opened. However, it closes!!
How can I avoid this?
BTW: I use Visual Studio 2010 and Winforms.
Use the FormClosing event from the Form, and the FormClosingEventArgs to cancel the process.
example:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = dialog = MessageBox.Show("Do you really want to close the program?", "SomeTitle", MessageBoxButtons.YesNo);
if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
Use the form's FormClosing event of your program window. You can then set e.Cancel to true if the user clicks no:
this.FormClosing += (s, e) => {
DialogResult dialog = dialog = MessageBox.Show("Really close?", "SomeTitle",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.No)
{
e.Cancel = true;
}
};
I guess you are using FormClosed. Are you? Then it's too late.
Try this
this.FormClosing += new FormClosingEventHandler(delegate(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you really want to exit this application?", MessageBoxButtons:=MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
});
Refer to Mudu's answer.
Basically unless you specify additional parameters to MessageBox.Show() you cannot get any result other than DialogResult.Ok from a default MessageBox.
The code you posted (minus your little typo of dialog = dialog =) works exactly as expected to me.
Also: Application.Exit() IS the correct way of closing an application :)

C# The exit message box pops up twice. I want to generate an event once.

bool ClosedFormMenu = false;
private void Cancel_btn_Click(object sender, EventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "program close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(dialog == DialogResult.Yes)
{
ClosedFormMenu = true;
Application.Exit();
}
}
private void Form_closing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "program close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if(!ClosedFormMenu)
{
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
return;
}
}
}
Press the X or Cancel button to display the exit message. Press 'Yes' on the message and it will appear again. I want to eliminate this phenomenon.
I would like to implement the program so that it will exit completely if I click cancel button or X button.
Answer Thanks in advance.
Thank you.
As soon as you call Application.Exit(), the event method Form_closing() will be called.
This is why your MessageBox appears twice.
You call it in the Cancel method, exit out of the application, the application calls Form_Closing() and the MessageBox appears again.
You should only call
Application.Exit();
in your Cancel_btn_Click Method.
You can reduce your code to the following:
private void Cancel_btn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form_closing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "program close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialog == DialogResult.No)
e.Cancel = true;
// TODO: Add 'else' if you want to call a cleanup
// method or do something before the application closes.
}
Why is 'return' missing in Form_closing()?
'return' is missing, because this is a method with the return type 'void'.
It is not expected to 'return' anything and since we do not need to exit out of the method prematurely, we do not need it.
Why is 'Application.Exit()' missing in Form_closing()?
The application has already received the command to exit. This is why it is closing the form. Therefor, we do not need it here.
Calling Application.Exit() from the button handler also calls Form_closing(). Having the dialog box in both the button handler and Form_closing() makes the question appear twice. Try this:
private void Cancel_btn_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form_closing(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to close the program?", "program close", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialog == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}

Exit my application with Yes No message box

I am using below code to exit my entire application application with YesNo question messagebox my problem is that some times I got the message twice and other times I get it correctly as one message display ..
any one can help me why that happening ??
private void AppClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void F0100_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
result = MessageBox.Show("Are you sure you want to exit?", "Exit Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//Environment.Exit(1);
Application.Exit();
}
else
{ e.Cancel = true; }
}
I am assuming that the form in question is your main form i.e. You launch this Form as Application.Run(new Form1());
If that is the case, typically you don't need to do Application.Exit() under the Yes branch from FormClosing. So your code should be something like below
private void F0100_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing)
return;
DialogResult result;
result = MessageBox.Show("Are you sure you want to exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
{
e.Cancel = true;
}
}
The superfluous Application.Exit() call, creates an extra FormClosing event
NOTE: You should also check the FormClosingEventArgs.CloseReason so you don't create an extra popup when say user is logging off or killing the process.
Then this answer might help you some. Basically, you need to use the one you commented out: Environment.Exit(0). The Application one is a graceful attempt at exiting that attempts to close forms. Your form is still open, so it receives a 2nd FormClosing call. It's all up to timing, really, but I'd think that most of the time you'd see the prompt twice.

Prevent Form from closing when DialogResult is No

I am using following code on closing event of my Windows forms application form:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
return;
}
else
Application.Exit();
}
But the problem is, whenever user clicks Yes the application ends but when user clicks No, the application is still running but the form hides. How can I keep form visible when user clicks No?
Update
Another problem I am facing is that, when I click Yes, the MessageBox displays again and then I click Yes the application exits. Why its displaying for two times?
You don't have to call Application.Exit() because if you do not cancel closing it program will exit itself. Below is corrected code that is working:
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult dr = MessageBox.Show("Are you sure you want to quit?", "Leaving App",MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.No)
{
e.Cancel = true;
}
}
private void Form1_Closing(Object sender, CancelEventArgs e) {
if (!isDataSaved) {
e.Cancel = true;
MessageBox.Show("You must save first.");
}
else {
e.Cancel = false;
MessageBox.Show("Goodbye.");
}
}
Source: MSDN
you need to cancel the exit using e.Cancel = true; in your event Main_FormClosing to stop from closing
I know it is not the correct way but it will result as you want.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(MessageBox.Show("Are you sure you want to quit?", "Leaving App", MessageBoxButtons.YesNo).ToString()=="No")
e.Cancel = true;
}
Because of ToString() method will provide the result of the dialog box when used with a message box.
I am Akhilavishnu, for more help please mail me akhilavishnu2#gmail.com. Please put these lines of codes in your application it works well happy coding.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(MessageBox.Show("Are you sure that you want to exit",
MessageBoxButtons.YesNo,
MessageBoxIcon.question) == DialogResult.No)
{
e.Cancel=true;
}
}
//in formclose event
private void Form1_FormClosed(object sender,FormClosedEventArgs e)
{
Application.Exit();
}
If you just put the following code into the click event for your exit button, it works fine :)
DialogResult quit = MessageBox.Show("Are you sure you want to quit?", "Quit", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
if (quit == DialogResult.Yes) this.Close();

Ask the user before closing C# WPF application

I want to ask the user before closing the application.
I'm using C# .NET 4.0 WPF. I can do it in windows forms, but not in WPF.
Event is fired when the user want to close the app. Message box appears, bun no matter which button is pressed (Yes or No) the application always closes. Why? Where is the mistake?
It works, but only when the user presses the "X". When the user presses the close button with Application.Current.Shutdown(); it is not working.
private void MainWindowDialog_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult result = MessageBox.Show("Do you really want to do that?",
"Warning", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No)
{
e.Cancel = true;
}
}
The Closing event cannot be cancelled if you call Application.Current.Shutdown(). Just call the Window.Close() method instead, which will give you a chance to veto the close operation. Once all your program's windows have closed the application will shutdown automatically.
For more information checkout the Application Management page on MSDN.
Just call YourMainWindow.Close() and use the Closing event as described before.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
e.Cancel = false;
else
e.Cancel = true;
}
Why don't you just ask the user whether he wants to close the application, and then call Application.Current.Shutdown() like this:
private void closeButton_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("Do you want to exit?", "Confirm",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}

Categories