Closing a form :)
-
I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?
Subscribe to the Form.Closing event :) top secret xacc-ide 0.0.1
-
I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?
OK :) I have actually found out how it is done. Here is what a I did (nothing specially, really). I happens to me too often the solution is obvious but I miss it. buttonExit_Click... etc... is an event handler... is it how you are going to do it? Thanx
private void buttonExit\_Click(object sender, System.EventArgs e) { // Raises the System.Windows.Forms.Form.Closing event this.OnClosing(new System.ComponentModel.CancelEventArgs()); } private void FormOfficeTools\_Closing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show(this, "Желаете ли да излезете от програмата?", "Изход от програмата", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question ); if (System.Windows.Forms.DialogResult.Yes == result) Application.Exit(); else // If "No" then cancel the System.Windows.Forms.Form.Closing event e.Cancel = true; } #endregion
-
Subscribe to the Form.Closing event :) top secret xacc-ide 0.0.1
sure but i've just missed that the
System.ComponentModel.CancelEventArgs.Cancel
property has to be set totrue
BTW is this call a good idea:this.OnClosing(new System.ComponentModel.CancelEventArgs());
Here I raise the
System.Windows.Forms.Form.Closing
event, when a button Quit is clicked on (or another exit condition is met). -
OK :) I have actually found out how it is done. Here is what a I did (nothing specially, really). I happens to me too often the solution is obvious but I miss it. buttonExit_Click... etc... is an event handler... is it how you are going to do it? Thanx
private void buttonExit\_Click(object sender, System.EventArgs e) { // Raises the System.Windows.Forms.Form.Closing event this.OnClosing(new System.ComponentModel.CancelEventArgs()); } private void FormOfficeTools\_Closing(object sender, System.ComponentModel.CancelEventArgs e) { System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show(this, "Желаете ли да излезете от програмата?", "Изход от програмата", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question ); if (System.Windows.Forms.DialogResult.Yes == result) Application.Exit(); else // If "No" then cancel the System.Windows.Forms.Form.Closing event e.Cancel = true; } #endregion
You don't need to call
Application.Exit
(in fact, I recommend you don't). The only condition you have to worry about is anything that would lead you to setCancelEventArgs.Cancel
totrue
. If you don't set it, the form will continue closing as normal. The .NET Framework SDK even has a good example for theForm.Closing
event.Microsoft MVP, Visual C# My Articles
-
You don't need to call
Application.Exit
(in fact, I recommend you don't). The only condition you have to worry about is anything that would lead you to setCancelEventArgs.Cancel
totrue
. If you don't set it, the form will continue closing as normal. The .NET Framework SDK even has a good example for theForm.Closing
event.Microsoft MVP, Visual C# My Articles
-
Thank you, Heath Stewart What's wrong with
Application.Exit
? Why don't you recommend using it? How should I then quit an application? I'm looking for theForm.Closing
event... :) and still can't find it.Because the form is already closing. If the form is your main form - the one you passed to
Application.Run
, once it's closed the application will exit anyway. There may already be messages in the pump that were posted that may need to clean-up resources. CallingApplication.Exit
circumvents that. So, if you don't setCancelEventArgs.Cancel
totrue
, the form will close and the application will exit just as if you hadn't handled theForm.Closing
event. And what do you mean you can't find it? It's documented in the .NET Framework SDK, and if you use the designer you can click the "Events" button in the PropertyGrid when your form is selected, find theClosing
event and double-click it. You can also use IntelliSense. The thing is, you're not looking forForm.Closing
, you're looking forthis.Closing
or_form1_.Closing
or whatever is appropriate for accessing your instance of your form. Some basic understanding of programming is assumed.Closing
is an instance event, so it will be defined for an instance of your form (so access it usingthis
or a variable that refers to yourForm
).Microsoft MVP, Visual C# My Articles
-
Because the form is already closing. If the form is your main form - the one you passed to
Application.Run
, once it's closed the application will exit anyway. There may already be messages in the pump that were posted that may need to clean-up resources. CallingApplication.Exit
circumvents that. So, if you don't setCancelEventArgs.Cancel
totrue
, the form will close and the application will exit just as if you hadn't handled theForm.Closing
event. And what do you mean you can't find it? It's documented in the .NET Framework SDK, and if you use the designer you can click the "Events" button in the PropertyGrid when your form is selected, find theClosing
event and double-click it. You can also use IntelliSense. The thing is, you're not looking forForm.Closing
, you're looking forthis.Closing
or_form1_.Closing
or whatever is appropriate for accessing your instance of your form. Some basic understanding of programming is assumed.Closing
is an instance event, so it will be defined for an instance of your form (so access it usingthis
or a variable that refers to yourForm
).Microsoft MVP, Visual C# My Articles
Sorry, I have meant the
Form.Closing
sample you have mentioned about and not the event! I have made a pretty stupid mistake and haven't checked what I have written. I understand aboutApplication.Exit
now. I thought that it isn't generally a good idea to useApplication.Exit
at all. BTW I have replacedApplication.Exit
with:private void buttonExit_Click(object sender, System.EventArgs e)
{
this.Close(); // Close the main form and exit the application
}Then I handle the
Form.Closing
event to ask the user if he really wants to quit etc. Is it a good idea or not? :) It seems to me like an implementation of your idea. -
Sorry, I have meant the
Form.Closing
sample you have mentioned about and not the event! I have made a pretty stupid mistake and haven't checked what I have written. I understand aboutApplication.Exit
now. I thought that it isn't generally a good idea to useApplication.Exit
at all. BTW I have replacedApplication.Exit
with:private void buttonExit_Click(object sender, System.EventArgs e)
{
this.Close(); // Close the main form and exit the application
}Then I handle the
Form.Closing
event to ask the user if he really wants to quit etc. Is it a good idea or not? :) It seems to me like an implementation of your idea.Yes, that is the way to handle it.
Form.Close
will send the Windows messages to close theForm
. You're handling theWM_CLOSING
message and optionally cancelling it. If you don't, theForm
continues to close. When it's closed, if the message pump was waiting on it (i.e., you passed an instance of thatForm
toApplication.Run
), then the application will exit.Microsoft MVP, Visual C# My Articles
-
sure but i've just missed that the
System.ComponentModel.CancelEventArgs.Cancel
property has to be set totrue
BTW is this call a good idea:this.OnClosing(new System.ComponentModel.CancelEventArgs());
Here I raise the
System.Windows.Forms.Form.Closing
event, when a button Quit is clicked on (or another exit condition is met).to Raise the Form.Closing event you need to just call: this.Close();
-
I cannot figure out how to ask the user for input if a form should be closed (or application exited), when alt+f4 or the X button is clicked? Is there an event being fired when the user clicks the X button (the one on the caption bar)?
Yes there is... it's called Closing... and when you handle it you can do all kind of stuff (including cancellation of closing) with it ;)