本文介绍了如何防止在C#中关闭HIDDEN表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我想防止表单关闭.我知道OnFormClosing的用法,但是当表单被隐藏(Visible == false)时,不会调用OnFormClosing方法.在这种情况下,有没有办法拦截表单关闭?

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?

编辑(更多详细信息):该表单是MdiParent中的子进程,应在后台保持不可见,并等待其他线程的调用(通过Invoke).

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).

当用户断开连接"时,MdiParent关闭所有子窗口,在这种情况下,以上表格应保持打开状态,但不可见,仍在等待呼叫.当MidParent本身关闭时,所有表格都应关闭.

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(没有解决方案?):似乎没有解决方案.现在,我的解决方法是在MdiParent代码中排除我不愿关闭的表单,该表单将关闭所有其他表单.

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.

推荐答案

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.

这篇关于如何防止在C#中关闭HIDDEN表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:12