How to detect when owner form c开发者_开发问答loses (from a control inside it)?
UPD I need the control to know that it's form is closing, not vice versa
Credits to Fredrik Mörk for this solution:
FindForm().FormClosing += parentForm_FormClosing;
You should intercept FormClosing event. In FormClosingEventArgs the variable CloseReason will show you why is the form closing. Your best bet is intercepting when this variable equals UserClosing enumerated value.
The form owner closing is when a form is closed by another parent form that can close the form or the form is closed when the parent form is closed.
Use the form closing event to check if another form closed the form:
private void AppMainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(e.CloseReason == CloseReason.FormOwnerClosing)
{
// do something
}
else
{
// do nothing
}
}
精彩评论