开发者

How to detect when owner form is closed from an inner control?

开发者 https://www.devze.com 2023-03-09 11:23 出处:网络
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 versaCredits to Fredrik Mörk for this solution:

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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消