How to prevent closing of a HIDDEN form in C#

ⅰ亾dé卋堺 提交于 2019-12-10 00:10:39

问题


I want to prevent closing of a form in some cases. I know the usage of OnFormClosing, but when the form is hidden (Visible==false), the OnFormClosing method is not called. Is there a way to intercept form closing in this case?

Edit (some more details): The form is a child in a MdiParent, should stay invisible in the background and wait for calls from another thread (by Invoke).

The MdiParent closes all child windows when the user "disconnects", in this case the above form should stay open, but invisible and still waiting for calls. When the MidParent itself is closed, all forms should close.

Edit2 (no solution?): It seems that there is no solution to this. My workaround now is to exclude my not-to-be-closed form in the MdiParent-code, that closes all other forms.


回答1:


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
                // This will cancel the event
                e.Cancel = true;
}

Regardless of the reason, this will effectively stop a form from closing.



来源:https://stackoverflow.com/questions/5375998/how-to-prevent-closing-of-a-hidden-form-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!