Prevent Form from closing when DialogResult is No - c#

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

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.

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.

Problems with events

I'm using a form with a OK and a Cancel button. When the user click on the Cancel button, the user will get a message to confirm if the form should close or not. A click on OK = close, but when click on Cancel, the form should not close, but thats what is happening right know, and I have tested to add some event code for the form, but still closing. What can I do to Get it to work properly?
// Button - Cancel
private void btnCancel_Click(object sender, EventArgs e)
{
// Message box to confirm or not
if (MessageBox.Show("Do you really want to cancel and discard all data?",
"Think twice!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
{
// Yes
//this.Close(); // Closes the contact form
m_closeForm = false;
}
else
{
m_closeForm = false;
// No
// Do nothing, the user can still use the form
}
}
private void ContactForm_Formclosing(object sender, FormClosingEventArgs e)
{
if (m_closeForm)
e.Cancel = false; // Stänger formuläret. Inget skall hända
else
e.Cancel = true; // Stänger inte formuläret
}
You can try the following, by adding the messagebox with the dialog result in the form closing event. I believe this is the better approach:
private void btnCancel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to cancel and discard all data?", "Think twice!",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
this.Close();
}
// Form wont close if anything else is clicked
}
private void btnOk_Click(object sender, EventArgs e)
{
// PerformAction()
this.Close();
}
I think this is what you are looking for.
I think you'll find that in your form's Designer.cs file you'll have the following line:
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
Remove this line and your form will no longer automatically close regardless of the result of your customer MessageBox.
To Cancel Closing you can use the FormClosingEventArgs Property, ie, e.Cancel to true.
Use this code in FormClosing Event of the Form.
if (MessageBox.Show("Do you really want to cancel and discard all data?",
"Think twice!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}

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