duplicated message when handling close button X - c#

I made an exit button as menu item with confirmation message
and below code do the job,.. but when I am trying to handle the X button I am getting the confirmation message twice I tried to comment the message on the exit button on the menu item but still getting the message twice when I click on YES
private void ExitMenuItem_Click(object sender, EventArgs e)
{
/*DialogResult result;
result = MessageBox.Show("are you sure?", "exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{ return; }*/
}
private void F0101_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result;
result = MessageBox.Show(""are you sure?", "exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{ e.Cancel = true; }
}

Application.Exit() sends a message to your application to close again, use:
Environment.Exit(0);
Application.Exit Docs:
Informs all message pumps that they must terminate, and then closes
all application windows after the messages have been processed.
Environment.Exit Docs:
Terminates this process and returns an exit code to the operating
system.
So using Environment, the process is terminated. Even you call it inside a try..finally block, the finally block doesn't execute.
However, Application.Exit can be used when you need a chance to execute state saving code in the closing events (which is in your case calling the same method and thus showing the method twice).

Related

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.

How can I stop running application in backgournd and exit without exception?

I Have many winforms in a project. When I click on Cross button to close one specific form maybe it is running in the background process. So How can I stop that from running in background when I click on cross button. I have the code for exiting application;
Application.Exit();
But this also shows exception when from frmMainMenu I click on logoutMenuStrip option. For the LogoutMenuStrip I have the following code to forcefully exit the application. But still it shows some exception. My Question is how can I Prevent all exception when I click on that logoutMenuStrip.
I supposed that maybe on clicking the other forms closing they get
running in background
Code for LogoutMenuStrip:
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show("Are you sure to QUIT ?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
Application.Exit();
}
catch
{
Application.Exit();
}
}
Thanks in advance :)
If you have multiple instances of your application running and one or more of those is in background, you can enumerate the processes and then kill them.
var processes=Process.GetProcessByName( "AppName" );
foreach(var process in processes )
process.Kill();
use Environment.Exit(0); it closes everything and shuts down your application
private void logoutToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
MessageBox.Show("Are you sure to QUIT ?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
Environment.Exit(0);
}
catch
{
Environment.Exit(0);
}
}

prohibit user from closing the form

I have a main form called Form_Main, when somebody wants to close it, it will close the entire application (by entire application I mean quitting other forms as well). Therefore I prepared a yes/no MessageBox that ask users if they really want to quit the form. Here's where I'm at:
private void Form_Main_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
Environment.Exit(1);
}
else
{
//does nothing
}
}
The "OK" button works. But when users clicks "Cancel", Form_Main is closed, but the application is still running (other forms are untouched). What should I replace //does nothing with?
Use the FormClosing event (instead of FormClosed), and then set e.Cancel = true:
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
{
var result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.OKCancel);
e.Cancel = (result != DialogResult.OK);
}
The FormClosing event occurs before the form actually closes, so you still have a chance to cancel it. By the time you get to the FormClosed event, it's too late.

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