Ask the user before closing C# WPF application - c#

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();
}
}

Related

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.

Why when choosing No it's asking twice?

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.

Ask confirmation message only once from user in C# Window Appliaction

i built an windows application using c# .
On form closing event i wrote a code like this to ask confirmation form user...
private void Approve_User_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you really want to QUIT application...?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
sometimes it ask four times , even upto five times....
I want this only once if user press Yes then application should exit.
I need your help and suggestions.
Thanks in advance.
Apparently you are subscribing to this Approve_User_FormClosing event in a place which execute a few times. If you subscribe 4,5 times it will execute 4,5 times.
If you just want to capture application exit event have a look at this thread.
Edit
You need something like below to achieve your weird requirement.
private bool isExiting = false;
private void Approve_User_FormClosing(object sender, FormClosingEventArgs e)
{
if (isExiting)
return;
if (MessageBox.Show("Do you really want to QUIT application...?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
isExiting = true;
Application.Exit();
}
else
{
e.Cancel = true;
}
}

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.

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();

Categories