close button

D

Darren Oakey

I have a non-modal form - anyone know how I can trap when the user hits the
close button?

thanx!
 
P

Peter Foot [MVP]

Assuming you mean the OK button, you can handle the Form.Closing event which
is called just before the form itself is closed (or the Closed event which
is called after the form has been closed). If you use Closing you can
override the form from closing if some criteria is not met. Here is a simple
example where the parent form displays a message box when the other form is
closed.
private void button4_Click(object sender, System.EventArgs e)

{

//create new instance of our form

Form2 frm2 = new Form2();

//register an event handler for the Closing event of sub-form

frm2.Closing+=new System.ComponentModel.CancelEventHandler(frm2_Closing);

//show the form non-modally

frm2.Show();

}



private void frm2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

//put your code here to react on the other form closing

MessageBox.Show("Second form was closed");

}



Peter


--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com
Handheld Interactive Reference Guides
 
D

Darren Oakey

ahh - you would think that, wouldn't you? Try it - it doesn't... The only
event I've found I get reliably is the deactivated event... but there are
too many other things that cause the deactived event to occur :(
 
P

Peter Foot [MVP]

That code was taken straight from a working vb test project. It is placed in
the calling form, when the subform ok button is clicked the frm2_Closing
method in the parent is called which displays the messagebox.

Peter

--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com
Handheld Interactive Reference Guides
 

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

Top