Back button affected by FormClosing event - c#

I want to go back to the form1 but everytime I click the back button, it triggers the FormClosing event so it prompts me if I want to quit the application. How can I solve this problem in order to exclude the other buttons from the FormClosing event?
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
this.Hide();
}

Best thing to do would be to set global variable that will tell closing event to show/not show that dialog.
Try something along these lines.
private bool CalledFromButton = false;
private void Form2_FormClosing_1(object sender, FormClosingEventArgs e)
{
if(CalledFromButton)
return;
DialogResult dialog = MessageBox.Show("Do you really want to quit?", "Exit",
MessageBoxButtons.YesNo);
if (dialog == DialogResult.Yes)
{
Application.Exit();
}
else if (dialog == DialogResult.No)
{
e.Cancel = true;
}
}
private void back_button_Click(object sender, EventArgs e)
{
CalledFromButton = true;
this.Hide();
}

Related

Close button is not closing the form properly

My form contains a button named as close. I just put the below code in the close button click... Then the 2nd code set is for the form closing. But if I execute this when I click the close button a MessageBox will appear. When I click the "Yes" button, the form is not closing but if I click the "Yes" button second time the form will be closed. What is the reason? Can you, please, help me?
private void btnClose_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// MessageBox.Show("The application has been closed successfully.",
// "Application Closed!",
// MessageBoxButtons.OK);
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
-------------------------------------
private void frminventory_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
System.Windows.Forms.Application.Exit();
}
else
{
this.Activate();
}
}
Do not close/exit Application, but Form:
private void btnClose_Click(object sender, EventArgs e) {
// On btnClose button click all we should do is to Close the form
Close();
}
private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
// If it's a user who is closing the form...
if (e.CloseReason == CloseReason.UserClosing) {
// ...warn him/her and cancel form closing if necessary
e.Cancel = MessageBox.Show("Are You Sure You Want To Close This Form?",
"Close Application",
MessageBoxButtons.YesNo) != DialogResult.Yes;
}
}
Edit: Usually we ask direct questions to user (it's unclear what wrong things can arise if I just "Close This Form"), e.g.
private void frminventory_FormClosing(object sender, FormClosingEventArgs e) {
// Data has not been changed, just close without pesky questions
if (!isEdited)
return;
// If it's a user who is closing the form...
if (e.CloseReason == CloseReason.UserClosing) {
var action = MessageBox.Show(
"You've edited the data, do you want to save it?"
Text, // Let user know which form asks her/him
MessageBoxButtons.YesNoCancel);
if (DialogResult.Yes == action)
Save(); // "Yes" - save the data edited and close the form
else if (DialogResult.No == action)
; // "No" - discard the edit and close the form
else
e.Cancel = true; // "Cancel" - do not close the form (keep on editing)
}
}
So, because i can't comment, i would do something like this.
//Create this variable
private bool _saved = true;
public Form1()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
DoSomething();
_saved = true;
}
//Raised when the text is changed. I just put this for demonstration purpose.
private void textBox1_TextChanged(object sender, EventArgs e)
{
_saved = false;
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_saved == false)
{
var result = MessageBox.Show("Are you sure you want to close?\nYou may have unsaved information", "Information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
_saved = true;
Application.Exit();
}
else
e.Cancel = true;
}
}
Hope this can solve yor problem

Form Closing dialog result appears in all forms

I have this Code in the Form1 closing event:
private void MemberForm_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult del = MessageBox.Show("Save changes?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (del == DialogResult.Yes)
{
// do something
}
if (del == DialogResult.No)
{
// do something
}
if (del == DialogResult.Cancel)
{
e.Cancel = true;
}
...
}
If I open form2 from button in my form1, in form2 closing event it will show me dialog result again. I want dialog result to be showed only in form1.
Why does this happen?
So my new question is: Can i do the same with save button? Preventing write the code second time?
Example
base.OnClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
DialogResult del = MessageBox.Show("Save change?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (del == DialogResult.Yes)
{
??????????????
}
if (del == DialogResult.No)
{
Form3 ss = new Form2();
Hide();
ss.ShowDialog();
}
if (del == DialogResult.Cancel)
{
e.Cancel = true;
}
public bool saveToolStripMenuItemClicked { get; set; }
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 ss = new Form3();
Hide();
ss.ShowDialog();
}
Now i want when dialogresult (Yes) do the same with saveToolStripMenuItem_Click
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
//Some Code
}
}
Try this, this should only run when you close the whole application, make sure you put it on the main form.
The reason why yours is running on Form2 closing is because you have a memberclosing which from my understanding runs on all forms closing
UPDATE
I have edited the code above, if you put the code in that if statement then it will only run then the user clicks the close button
UPDATE 2
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
if(BackButtonClicked)
{
//code used for DialogResult.No answer
BackButtonClicked = false;
}
if (e.CloseReason == CloseReason.UserClosing)
{
//Some Code
}
}
public bool BackButtonClicked { get; set; }
private void backButton_Click(object sender, EventArgs e)
{
//Some code
BackButtonClicked = true;
//Some code, close form
}

Click "yes" double when closing form C#?

I write a code for event close form C#. It works, but when I click "yes" to close form I must click twice.
What wrong with it? And how can I fix this problem?
Here is my code
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
Your logic right now will run e.Cancel = true if you click yes, as in it cancels the closing.
Also, as mentioned in the comments, Application.Exit() is not necessary.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result != DialogResult.Yes)
e.Cancel = true;
}
A simpler version would be something like this:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (MessageBox.Show("Sure?",
"Exit",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes);
}
If the use clicks the Yes button, then e.Cancel will be set to true.
If e.Cancel is set to true, the form will not close.
otherwise let the form closing sequence run it's course.
Calling the Application.Exit will trigger the FormClosing event again. The solution is easier than you think:
if (result == DialogResult.No)
{
e.Cancel = false; //It works as you expected
}
else
{
e.Cancel = true;
}

How to make Exit confirmation yes or no on Control Button using C# Window Form

I have a one issue in my window application, there are two form :login and main.
I have created Exit Button on both form and also coded of Exit Confirmation.
It works fine when we Press Exit Button message No on form 1. But We login to on form to
another form and second form Exit Button message button press No , and then back to return
form one and then click exit btton No ::::::it will display two times message PopUp of confirmation...............
Code
private void Btn_Exit_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("Do you want to exit.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
Application.ExitThread();
} else
{ }
}
Plz there is any solution in c#............
Just code this.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
}
In your case consider LoginForm and MainForm , Once you logged in you try to hide the LoginForm and show MainForm. Problem is when you try to close MainForm it will invoke the respective FormCloseEvent and once you chose to close, it automatically invoke parent form that is hiding in background, so it invokes LoginForm's FormCloseEvent. This is the reason for two time Popups.
To resolve this you need to trigger an event i.e whenever Child form is closed you need to raise a flag, so in your parent's FormCloseEvent you need to check for flag, if flag is true you no need to show the pop up.
private bool isMainFormClosed = false;
private void showMainForm()
{
// Hide the loginform UI.
this.Hide();
var mainForm = new MainForm();
// Creating close event for mainform, whenever close icon is clicked it will close the login form which is hiding in background.
mainForm.FormClosed += new FormClosedEventHandler(mainFormClosed);
// Show the mainform UI
mainForm.Show();
}
private void mainFormClosed(object sender, FormClosedEventArgs e)
{
this.isMainFormClosed = true;
this.Close();
}
private void loginFormClosing(object sender, FormClosingEventArgs e)
{
if(!this.isMainFormClosed)
{
DialogResult dialogResult = MessageBox.Show("Do you want to close the application",AppConstants.APPLICATION_NAME,
MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(dialogResult == DialogResult.No)
e.Cancel = true;
}
}
DialogResult is returned by dialogs after dismissal. It indicates which button was clicked on the dialog by the user.
Code
private void btnExit_Click(object sender, EventArgs e)
{
const string message = "Do you want to exit?";
const string caption = "EXIT";
var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else if (result == DialogResult.Yes)
{
this.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to close this application?", "Exit", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
{
e.Cancel = true;
}
}

Cancel Application Exit

My application is closing when I press Alt + F4. How I will do to have a MessageBox to show first for confirmation before exiting and if No is response the applcation will not continue to close?
In addition to the answers already posted here, don't be the dork that hangs the complete system:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing)
{
e.Cancel = false;
return;
}
// other logic with Messagebox
...
}
Handle the Form.Closing event, which takes a CancelEventArgs as parameter. In that handler, show your messagebox. If the user wishes to cancel, set the property .Cancel of the event args to true, like this:
private void Form1_Closing(object sender, CancelEventArgs e)
{
var result = MessageBox.Show("Do you really want to exit?", "Are you sure?", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
e.Cancel = true;
}
}
On FormClosing() event add this code:
private void MyForm_Closing(object sender, CancelEventArgs e)
{
if(MessageBox.Show("Are you sure want to exit the App?", "Test", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}

Categories