How to assign/call a function when a "Close" button of a Form is cliecked?

  • Thread starter Thread starter Carla Simeoni
  • Start date Start date
C

Carla Simeoni

Assume I have an open Form.

Then I click the "normal" window "X" icon (=Close button). How do I assign a function in Form.cs
to this event ? In other words if this Form/window is closed I want to perform some exit statements.

When I doubleclick on a "normal" button I am autoamtically taken from the designer pane
into the source code editor pane and a new function is automatically inserted.

This is not the case for a system button like the "close" window button.

Carla
 
Carla,

In order to do this, you want to attach an event handler to the Closed
event of the form. In order to do this in the designer, select the form
(not a control on the form) and look in the properties window. Click on the
thunderbolt, and you will see a list of events for the form. Double click
on the "Closed" one and the designer will create an event handler for you if
it doesn't already exist, or take you to the existing one if it does.
 
Assume I have an open Form.

Then I click the "normal" window "X" icon (=Close button). How do I
assign a function in Form.cs
to this event ? In other words if this Form/window is closed I want to
perform some exit statements.

Find the "Closing" event in the event list for the form's properties, and
assign your method there. If you want the designer to create a new method
stub for you, double-click in the empty box next to the event.

There are *lots* of events you can assign this way. Double-clicking on a
control itself only ever gets you the single default event for the
designer. The rest of the events, you have to attach from the properties
list (or write the code manually, of course).

Pete
 

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