When does 'e.Cancel = true' in FormClosing NOT prevent logoff? - c#

I have a Winforms app that minimizes to the Taskbar when a user clicks on the X. It does not prevent a user from logging off.
On the other hand, I have an app that shows a dialog when a user clicks the X, and it does prevent logging off showing:
"This app is preventing you from signing out."
I've tried tracking down what exactly is the difference between them but haven't found the difference. I thought it would be the dialog, but a test app has shown that in any case that the app sets e.Cancel = true; in FormClosing - the test app prevents logoff. This doesn't matter if the app shows a dialog or not, and if ShowInTaskbar is true or false. And both apps and the test app only Cancel if e.CloseReason == CloseReason.UserClosing.
So basically what I'm asking is when does e.Cancel = true not prevent logging off?

This doesn't matter if the app shows a dialog or not... And both apps and the test app only Cancel if e.CloseReason == CloseReason.UserClosing
That's the whole point, it doesn't matter whether you show a dialog or not. In the first app, you're not calling e.Cancel == true; at all because e.CloseReason is not UserClosing, it is WindowsShutDown.
According to the documentation:
UserClosing
The user is closing the form through the user interface (UI), for
example by clicking the Close button on the form window, selecting
Close from the window's control menu, or pressing ALT+F4.
You can confirm that it is in fact WindowsShutDown, by running some simple test like the following:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.IO.File.WriteAllText(#"D:\SomePath\CloseReason.txt", e.CloseReason.ToString());
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = true;
}
If you want to prevent the shutdown/log-off, you can either set e.Cancel to true without a condition, or by checking if e.CloseReason == CloseReason.WindowsShutDown:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// e.Cancel = true;
// Or..
if (e.CloseReason == CloseReason.UserClosing ||
e.CloseReason == CloseReason.WindowsShutDown)
{
e.Cancel = true;
}
}
But, please use this responsibly and be aware that the user can always force the shutdown/log-off as explained in this answer.
Edit:
If, on the other hand, you have some logic which might prevent the form from closing (i.e., by calling e.Cancel = true;) and you don't want that logic to apply when shutting down (or logging off), I always use something like the following:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown) return; // Go ahead and close.
if (!SafeToClose())
{
// Optional:
if (MessageBox.Show("Important process is running. Stay?",...) == DialogResult.Yes)
{
e.Cancel = true;
}
// Or directly:
//e.Cancel = true;
}
}

Related

How do i close shut down my application from another Form?

I have this event in a new Form:
private void CrawlLocaly_FormClosed(object sender, FormClosedEventArgs e)
{
}
Im not sure if to use Closed or Closing.
The reason is that i want to check if the user shut down the program for example by just closing the application from the taskbar.
If he did close it from the taskbar mouse right click then close then i want it to close all the program and not only this Form.
How can i do it ?
Application.Exit();
Will shut down your application.
Im not really sure if you can detect if he closed it via rightmouse menu. As far as I know you can only see the reasons provided in the FormClosedEventArgs. FormClosing will provide you with same reasons.
Use this event:
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason == CloseReason.UserClosing)
{
//close forms
Application.Exit();
}
}
There is no way to check whether user closed your form by clicking 'X' or through TaskBar or any other way as the result of CloseReason will always be CloseReason.UserClosing
Well I Stick with such issue also.
In mine app I have 2 forms, #1 main, #2 - for settings - if user close it i want to know save settings or not. Also if settings are null - close app not only form, if user click button save - I want to close (hide) #2 form.
So where is my solution we set tag value to 1 if click button save, so we will know "who" try to close form:
Predefined:
btnSave.Tag = 0;
On save button click event:
btnSave.Tag = 1;
this.Hide();
its will trigger onclose event:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
if (btnSave.Tag.ToString() == "0")
{
DialogResult dlg = MessageBox.Show("Do you want to exit without finished setup connection?", "Form", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dlg == DialogResult.No)
{
e.Cancel = true;
}
else
{
e.Cancel = false;
this.Dispose();
Application.Exit();
}
}
else
{
this.Hide();
}
}

Preventing Windows from shutting down

I am using windows form,I want to display the message to the user that the process is not complete if the user tries to shut down the windows or tries to closes the application before completion of process(the user forgot to complete the process),if the user presses OK i want to stop the window from shutting down and let the user complete the process,I did find some code on the net to do so but it is in VB not in c#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
{
Microsoft.VisualBasic.Interaction.Shell("shutdown -a", AppWinStyle.MinimizedFocus, false, -1);
MessageBox.Show("Shutdown process cancelled!");
}
}
Better to use this piece of code snippet instead, play with e.Cancel as per your requirement:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason.Equals(CloseReason.WindowsShutDown))
{
if (MessageBox.Show("You are closing this app.\n\nAre you sure you wish to exit ?", "Warning: Not Submitted", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Stop) == DialogResult.Yes)
return;
else
e.Cancel = true;
}
}
Reference: SystemEvents.SessionEnding Event
Occurs when the user is trying to log off or shut down the system.

How do I catch the event of exiting a Winforms application?

If the user wants to exit the application by clicking on the exit icon or by ALT+F4, I'd like to make a dialog box questioning the user if he/she is really sure about exiting.
How do I capture this event before the application is actually closed?
Check out the OnClosing event for the form.
Here is an extract from that link actually checks for a change on a text field and prompts a save:
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...
}
}
}
You can change the text to suit your needs, and then also I think you might want to switch the DialogResult.Yes to DialogResult.No based on your text.
Here is some modified code just for you:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("Are you sure you want to quit?", "My Application", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
You should subscribe to the Form_Closing event
Post a dialog box there, and if user abort the closing set the FormCloseEventArgs.Cancel to true.
For example in the Form_Load or using the designer, subscribe the event
Form1.FormClosing += new FormClosingEventHandler(Form1_Closing);
....
private void Form1_FormClosing(Object sender, FormClosingEventArgs e)
{
DialogResult d = MessageBox.Show("Confirm closing", "AppTitle", MessageBoxButtons.YesNo );
if(d == DialogResult.No)
e.Cancel = true;
}
Depending on the situation is not always a good thing to annoy the user with this kind of processing.
If you have valuable modified data and don't want the risk to loose the changes, then it is always a good thing to do, but if you use this only as a confirm of the close action then it's better to do nothing.
You can handle the Form_Closing or Form_Closed events for this.
In Visual Studio click the lightning bolt icon and scroll down to these events in the form properties list. Double click on the one you want and it will hook up the event for you.
Read msdn sample: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closing.aspx
Is this just one form? If so, you might want to use the FormClosing event, which allows you to cancel it (show the dialog, then set CancelEventArgs.Cancel to true if the user chooses to cancel closing).
If you're talking about windows forms, should be enough to catch your MainWindow's
FormClosing event and in case you want prevent closing, just set the argument of the event handler got fired, to true.
Example:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("Do you really want to exit?", "My Application",
MessageBoxButtons.YesNo) == DialogResult.No){
// SET TO TRUE, SO CLOSING OF THE MAIN FORM,
// SO THE APP ITSELF IS BLOCKED
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();
}
}

Disable the Close Button until the user clicks on the LogOff MenuItem

I am creating an application for Library Management Systems.
I want to disable the close button initially and enable it once the user clicks on the menu
item logoff.
Is there any way I could accomplish this functionality in my application?
I tried using the following code in the formClosing event but it is not working out.
private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
if (checkLogOff == false)
{
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = false;
this.ControlBox = false;
return;
}
else
{
this.ControlBox = true;
e.Cancel = true;
}
}
The value of checkLogOff Variable is set as follows:
public bool checkLogOff = false;
private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
{
checkLogOff = true;
/*Code to update the logoff users in the database*/
}
When executing the application if I dont click on the LogOff menu item I am getting the dialog box but immediately after I press the OK Button in the Message Box the Application closes. But I dont want to allow the user to close the application before clicking on the LogOff MenuItem.
Please help me out in accomplishing this task.
Thanks in advance!
I would not fiddle with the ControlBox property for this purpose, since it also removes the Maximize and Minimize buttons. The main issue with your code is that you should set Cancel to true in FormClosing to prevent the form from being closed. I think your code can be reduced to this, and still achieve what you want (given that we don't touch ControlBox):
private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = !checkLogOff;
if (e.Cancel)
{
MessageBox.Show("Please Log Off before closing the Application");
}
}
Change this:
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = false;
this.ControlBox = false;
return;
To this:
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = true;
this.ControlBox = false;
return;
You aren't canceling the form load.
When checkLogOff == false, you want to cancel the event, so you should set e.Cancel to True,
and to False when checkLogOff == true.
You inverted the setting of e.Cancel
it must be:
e.Cancel = true;
when you need to prevent the application from closing.
==>
private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
if (checkLogOff == false)
{
MessageBox.Show("Please Log Off before closing the Application");
e.Cancel = true; // <--
this.ControlBox = false;
return;
}
else
{
this.ControlBox = true;
e.Cancel = false; // <--
}
}
I know that is not the answer you seek, but telling a user to go to another menue item to log of, if he just wants to close the application is cubersome.
Why tell him he needs to log off? Cant you just call the Logoff function on his behalf, or display a dialog that asks if he wants to log off?
I highly suggest you do a test against e.CloseReason, and only perform your cancel-logic if it's a user closing action, otherwise your application will prevent a system shutdown, and an 'End this task now' dialog isn't a good impression on your users.

Categories