Windows Develop Bookmark and Share   
 index > Windows Forms General > FormClosing Event
 

FormClosing Event

Hi Every body
I have a form and i don't want to let the user close it by ALT+F4 or Click on the close button.
I know i should handle FormClosing event but how?
When i use FormClosingEventArgs and write this code :
if ( e.CloseReason = CloseReason.UserClosing )
{
e.Cancel = true;
}
I can't close it by calling the close Method.
I just wanna close the form by writing codes and not allow the users to close it manually.
like SplashScreen forms or something like that.Oh by the way is there any article about creating splashScreen forms in C#?

thank you.
Milad.a.p  Wednesday, August 09, 2006 10:07 AM
Unfortunately, the CloseReason enum doesn't distinguish between the user closing the form and your code closing the form. A bit of oversight I'd say but there is probably a Windows restriction that forces that. The workaround is pretty straight-forward:

private bool mItsMeClosingIt;
...
public sub CloseForm() {
mItsMeClosingIt = true;
this.Close();
}
...
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = !mItsMeClosingIt;

nobugz  Wednesday, August 09, 2006 2:46 PM
if ( e.CloseReason == CloseReason.UserClosing )
Alex Farber  Wednesday, August 09, 2006 10:15 AM
Sorry it was a writing mistake!!
I've written it exactly like this i know it should work but when i call the close method it doesn't work!! No exception no Error nothing .
Milad.a.p  Wednesday, August 09, 2006 12:22 PM
Unfortunately, the CloseReason enum doesn't distinguish between the user closing the form and your code closing the form. A bit of oversight I'd say but there is probably a Windows restriction that forces that. The workaround is pretty straight-forward:

private bool mItsMeClosingIt;
...
public sub CloseForm() {
mItsMeClosingIt = true;
this.Close();
}
...
if (e.CloseReason == CloseReason.UserClosing) e.Cancel = !mItsMeClosingIt;

nobugz  Wednesday, August 09, 2006 2:46 PM
Thank you.
Milad.a.p  Wednesday, August 09, 2006 4:48 PM

You can use google to search for other answers

Custom Search

More Threads

• Newbie Layout Question
• Making Shared assembly accessable to VB6 and unmanaged code
• Newbie: Need help to fill label with single item from dataset
• Customize existing native Windows controls
• create shortcut to forder from openfiledialog
• a word Pad ( windows application)
• Flash button not work in WebBrowser component using Framework 2.0
• Separator Line
• C# 3.0 - Call Windows Form from Console Project
• WebBrowser Control NavigateError event