Button with X in the top right corner of the form, how to catch this @ C # event

What event gets fired when I close the form using the X button? I want the event to fire only when the X button is pressed; I know there is a FormClosing event, but the problem is that it fires every time the form is closed ... It also fires when executed frm.close()

, and I don't want that to happen.

+2


source to share


4 answers


A specific X-related event is missing in the upper right corner of the form.



Use the FormClosing event instead . It has a Cancel parameter, which you can set to true

if you don't want the form to close. This allows you to test the closings of forms that occur by other means, such as clicking the OK button.

+8


source


You can check the CloseReason property of the FormClosingEventArgs parameter. This is the CloseReason.UserClosing closing when you click the "X" button.



+8


source


As Robert stated, there is no specific event associated with "X", but you basically have two options that you can use to resolve your question.

(i) Closing the Form . This event occurs when you press "X" but before the form is closed. So you can use this event handler to do some things just before the form closes. For example, you can stop closing / destroying a form with e.Cancel ();

(ii) Closed Form - This event occurs when the form is closed.

Hello

+2


source


I am using a form OnClosing event which can be canceled as well.

+1


source







All Articles