Problems with events - c#

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

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

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.

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.

How to handle form closing events

I am using Windows Forms in c# and have problems with how and when the the different forms (i have 2) should close and not. It is very annoying since i feel that i should be able to fix it. But here we go.
I have two forms, one MainForm that calls another form called ContactForm.
The MainForm:
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm frmContact = new ContactForm();
int index = lstCustomers.SelectedIndex;
//If a customer is selected, export data for the selected customer to ContactForm
if (index != -1)
{
frmContact.ContactData = customerMngr.GetCustomer(index).ContactData;
}
if (frmContact.ShowDialog() == DialogResult.OK) //Show the ContactForm object
{
//The user has chosen the OK button - add the new customer object
customerMngr.AddCustomer(frmContact.ContactData); //??
UpdateCustomerList();
}
if (frmContact.ShowDialog() == DialogResult.Cancel)
{
return;
}
}
And the form that is called:
OK button.
private void btnOK_Click(object sender, EventArgs e)
{
if (ValidateInput())
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
Cancel button:
private void btnCancel_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you want to cancel and discard all data?", "Cancel input", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
When the OK button in the ContactForm is used i want it to close, which works. When i press the cancel button, and no (in the box that appears), i want the form to stay open with the input still intact. Right now it doesn´t work.
Any ideas?
/Martin
Your code is alright. I think the problem lies in your Cancel Button itself. By this I mean that you probably attached (by designer or somewhere in code) DialogResul.Cancel to your button btnCancel.DialogResul property. To fix this simply set it to DialogResult.None.
If I'm right this is what is closing your second form.
See MSDN for more information.

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