form_unload

  • Thread starter Thread starter Ringo
  • Start date Start date
R

Ringo

How do I get code to run when I close a form? I know I can have a
button and put the code there then close the form that way, but I have
code that needs to run when the user clicks the X in the upper right of
the form. In VB6 I think it is the form_unload event, but that does not
seem to be the case in C#. What am I missing?
Thanks
Ringo
 
Ringo said:
How do I get code to run when I close a form? I know I can have a
button and put the code there then close the form that way, but I have
code that needs to run when the user clicks the X in the upper right of
the form. In VB6 I think it is the form_unload event, but that does not
seem to be the case in C#. What am I missing?

You can either handle the Closing or Closed event (or override OnClosing or
OnClosed). What's the difference? OnClosing occurs before the form closes
and gives you the option of cancelling the close if you want to do so.
OnClosed occurs past any opportunities to cancel the action.
 
I created a funtion:
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("I'm closing");
Console.Write("shutting down communication");
}

but I don't know how to get it to fire when the form closes?
Thanks
Ringo
 
I created a funtion:
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("I'm closing");
Console.Write("shutting down communication");
}

but I don't know how to get it to fire when the form closes?
Thanks
Ringo

If you don't subscribe to the event it won't fire. Creating it is not all you
have to do. Read the documentation on Events and see if you can figure out how
to use them. If not ask again.
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Ringo said:
I created a funtion:
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("I'm closing");
Console.Write("shutting down communication");
}

but I don't know how to get it to fire when the form closes?

That's very VB6 style. This is C# and things are different. Simply
creating a function with the name of the form concatenated with the name of
the event is not enough. You have two options. The first is to override
OnClosing. This is my preferred way of doing this. Look up how to override
a method for info on how to do that. Your second option is to go with an
event handler. You need one more line of code to do that. If you look at
how your Form1_Load is hooked up you should be able to figure it out
quickly.
 
Tom Porterfield said:
That's very VB6 style. This is C# and things are different. Simply
creating a function with the name of the form concatenated with the name
of the event is not enough. You have two options. The first is to
override OnClosing. This is my preferred way of doing this. Look up how
to override a method for info on how to do that. Your second option is to
go with an event handler. You need one more line of code to do that. If
you look at how your Form1_Load is hooked up you should be able to figure
it out quickly.


May I ask why overriding is your prefered way of doing it? If you use the
designer/property window you will get the eventhandler, why shouldn't you
want to use it?

Performance considerations or just keeping your code clean of all the Event
+= code?

Wouter de Kort :)
 
Wouter de Kort said:
May I ask why overriding is your prefered way of doing it? If you use the
designer/property window you will get the eventhandler, why shouldn't you
want to use it?

Performance considerations or just keeping your code clean of all the
Event += code?


It's more inline with OO methodologies and gives you greater control over
when your code runs. When you attach an event handler your code runs based
on what order the event handlers are attached. When you override, you can
choose to execute your code before the event handlers are fired, or after.
Or you can choose not to call the base at all which would prevent any event
handlers from being executed (under normal circumstances you wouldn't do
this last item but there are times when it may be necessary).

This is also the recommended approach in the .NET documentation. For many
On* methods whose implementation in the base class is to call delegates
you'll see a comment similar to the following:

"The OnClosing method also allows derived classes to handle the event
without attaching a delegate. This is the preferred technique for handling
the event in a derived class."
 
Tom Porterfield said:
It's more inline with OO methodologies and gives you greater control over
when your code runs. When you attach an event handler your code runs
based on what order the event handlers are attached. When you override,
you can choose to execute your code before the event handlers are fired,
or after. Or you can choose not to call the base at all which would
prevent any event handlers from being executed (under normal circumstances
you wouldn't do this last item but there are times when it may be
necessary).

This is also the recommended approach in the .NET documentation. For many
On* methods whose implementation in the base class is to call delegates
you'll see a comment similar to the following:

"The OnClosing method also allows derived classes to handle the event
without attaching a delegate. This is the preferred technique for handling
the event in a derived class."


ok, thanks for the explanation :)

Strange that the designer uses the event modell if the docs say that
overriding is recommended. If someones dubbleclicks on a button he will end
up using the event and not the overriding stuff. Is there a reason for?

Wouter
 
ok, thanks for the explanation :)

Strange that the designer uses the event modell if the docs say that
overriding is recommended. If someones dubbleclicks on a button he will
end up using the event and not the overriding stuff. Is there a reason
for?

For buttons, handling the click event is your only option as you are not in
a class derived from button. In your form class you cannot override the
OnClick method of a button. In your form class, which does derive from
System.Windows.Forms.Form you can override these methods and it's often
useful to do so for things like OnLoad, OnClosing, OnKeyPress, OnActivated,
etc. But that is overriding the method for the form, not for any specific
control on the form. I've never found a need to override the forms OnClick
method but you could if you had a need to do so.

For the controls on the form you still need to use event handlers to be able
to have code execute when those events occur on those controls, such as
clicking a button.
 
Tom Porterfield said:
For buttons, handling the click event is your only option as you are not
in a class derived from button. In your form class you cannot override
the OnClick method of a button. In your form class, which does derive
from System.Windows.Forms.Form you can override these methods and it's
often useful to do so for things like OnLoad, OnClosing, OnKeyPress,
OnActivated, etc. But that is overriding the method for the form, not for
any specific control on the form. I've never found a need to override the
forms OnClick method but you could if you had a need to do so.

For the controls on the form you still need to use event handlers to be
able to have code execute when those events occur on those controls, such
as clicking a button.

Hehe :) ok... that was a silly question :P

Wouter
 
Thanks for the help. I did an override on onclosing and it does just
what I want. It is funny that it is so obvious I'm a vb guy :-)
Thanks again,
Ringo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top