C# Application Restart - c#

I have a log-out option on my WinForms Application that uses this code:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
Clicking the Log out option brings up the "are you sure" box with "Yes" or "No"
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
, yes works no problem, but clicking "No" still restarts the application, how do i fix this?

Put the dialog box like this inside the MenuItem_Click:
// restart the application once user click on Logout Menu Item
private void eToolStripMenuItem_Click(object sender, EventArgs e)
{
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
Application.Restart();
}
}
Leave your FormClosing event empty:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
}
The other way of doing it would be if you absolutely want the dialog box to be implemented in the FormClosing event to override the OnFormClosing()
You can do this like this:
protected override void OnFormClosing(FormClosingEventArgs e) {
//Double check if user wants to exit
var result = MessageBox.Show("Are you sure you want to exit?", "Message",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
e.Cancel = true;
base.OnFormClosing(e);
}
In this case also the FormClosing event will remain empty.

DialogResult confirm = MessageBox.Show("confirm Exit?", "exit", MessageBoxButtons.YesNo);
if (confirm==DialogResult.Yes)
Application.Restart();
else
{
//do some thing
}

Related

Back button affected by FormClosing event

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

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 do I prevent a form from closing and display a confirmation dialog?

I want that when the user closes the window, a MessageBox shows up and asks if the user is sure he wants to close the window. But when I try, the window just closes and nevers shows me the MessageBox.
private void SchetsWin_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
MessageBox.Show("Example");
}
}
Instead of wiring an event for the form itself, just override the OnFormClosing method. As far as displaying a confirmation message to confirm it, just check the value of the DialogResult of the MessageBox:
protected override void OnFormClosing(FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
if (MessageBox.Show("Do you want to close?",
"Confirm",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.No) {
e.Cancel = true;
}
}
base.OnFormClosing(e);
}
Be careful with functions like this though — it has a tendency to annoy the end user.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure you want to Close?","Confirm",MessageBoxButtons.YesNo,MessageBoxIcon.Question) == 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;
}
}

How to prevent or block closing a WinForms window?

How can I prevent window closing by showing a MessageBox? (Technology:WinForms with C#)
When the close event occurs I want the following code to be run:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
//close addFile form
} else {
//ignore closing event
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
var window = MessageBox.Show(
"Close the window?",
"Are you sure?",
MessageBoxButtons.YesNo);
e.Cancel = (window == DialogResult.No);
}
Catch FormClosing event and set e.Cancel = true
private void AdminFrame_FormClosing(object sender, FormClosingEventArgs e)
{
var res = MessageBox.Show(this, "You really want to quit?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (res != DialogResult.Yes)
{
e.Cancel = true;
return;
}
}
A special twist might be to always prevent just the user closing the form:
private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (e.CloseReason == CloseReason.UserClosing);
// disable user closing the form, but no one else
}
Within your OnFormClosing event you can show the dialog and if answer is false (to not show) then set the Cancel property of the EventArgs to true.
For prevent or block the form closing in particular situation you can use this strategy:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (FLAG_CONDITION == true)
{
MessageBox.Show("To exit save the change!!");
e.Cancel = true;
}
}
Straight from MSDN:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// Determine if text has changed in the textbox by comparing to original text.
if (textBox1.Text != strMyOriginalText)
{
// Display a MsgBox asking the user to save changes or abort.
if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.Yes)
{
// Cancel the Closing event from closing the form.
e.Cancel = true;
// Call method to save file...
}
}
}
In your case you don't need to do anything to explicitly close the form. Unless you cancel it it will close automatically, so your code would be:
private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );
if (closeMsg == DialogResult.Yes) {
// do nothing
} else {
e.Cancel = true;
}
}
You can run any code you want when closing the form then, hide the form instead of closing it to prevent it from being disposed
yourFormName.FormClosing += (s, e) =>
{
// any code you want
yourFormName.Hide(); // this hides the form
e.Cancel = true; // this cancels the close event, you can put any boolean exprission
};

Categories