x button on a form

rjhe22

Well-known member
Joined
May 22, 2008
Messages
88
Programming Experience
Beginner
i want to put code behind the X button on the form. does any one no how to do this
 
ok. aslo i have a cancel button im putting the same code behind both. will that cause a conflict

Depends on what your code is doing

I'm fairly certain that the formclosing and formclosed events are called even when you close the form programaticaly.

Yes, they are fired whenever the form closes which is the only way to know when the Form's close button (The 'X' in the top right corner). But logically if the form's closing because of a button or because of that 'X' in the title bar you would want to run the form's closing code anyways.
 
the code is basically making sure people dont press the cancel button without saving there stuff. so if the press the cancel button and the system nos there was a change, a message box pop up asking them do they want to save there changes. but if i press the x button it does not save it or no pop up comes up. so in short need both the x button and the cancel button to do the same thing without any conflict
 
There is a e.CloseReason of FormClosing event that you can look into (if you need a reason). But if your cancel button just close the form you can take it from FormClosing for any reason.
 
Maybe you can just handle the FormClosing event and have the cancel button close the form without any verification. That way, the FormClosing event will handle all that only once no matter how the form was closed. And you can use the event's arguments to cancel the form closing if the user wants to continue working.
 
Back
Top