How to determine when the owner form is closed from an internal control?

How to determine when the owner form closes (from the control inside it)?

UPD I need a control to know that it is closing, and not vice versa

+7
source share
3 answers

Fredrik MΓΆrk loans for this solution:

FindForm().FormClosing += parentForm_FormClosing; 
+13
source

You must catch the FormClosing event. In FormClosingEventArgs, the CloseReason variable will show you why the form is closing. It is best to intercept when this variable is equal to a UserClosing enumerated value.

+3
source

Closing the form owner is when the form is closed by another parent form, which can close the form or the form closes when the parent form is closed.

Use the form close event to check if another form has closed the form:

 private void AppMainForm_FormClosing(object sender, FormClosingEventArgs e) { if(e.CloseReason == CloseReason.FormOwnerClosing) { // do something } else { // do nothing } } 
0
source

All Articles