问题
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