简体   繁体   English

使用CloseReason的FormClosing = UserClosing无法正常工作?

[英]FormClosing with CloseReason = UserClosing doesn't work expectedly?

I have a main form, in the class of this form, I declare another form. 我有一个主窗体,在此窗体的类中,我声明了另一种窗体。 This form lives with the main form until the main form is unloaded. 该表单与主表单一起存在,直到卸载主表单为止。 There is a button on the main form, clicking this button will show the member form (I mentioned above). 主窗体上有一个按钮,单击此按钮将显示成员窗体(我在上面提到)。 I want to prevent the member form from closing when user closes that form and I added the following FormClosing event handler for that form: 我想防止成员表单在用户关闭该表单时关闭,并为该表单添加了以下FormClosing事件处理程序:

private void MemberForm_FormClosing(object sender, FormClosingEventArgs e) {
   if(e.CloseReason == CloseReason.UserClosing){
       e.Cancel = true;
       Hide();
   }
}

That works OK for that form. 对于该表格,这可以正常工作。 However if user closes the main form, this form is not closed, and it's hidden somewhere making my application seem to run silently. 但是,如果用户关闭主窗体,则此窗体不会关闭,并且隐藏在某个地方,使我的应用程序似乎无提示运行。 I want this form also to be closed. 我希望此表格也能关闭。 This is very simple by adding some FormClosed event handler for my main form to close the member form manually. 通过为我的主窗体添加一些FormClosed事件处理程序以手动关闭成员窗体,这非常简单。 Closing it manually is OK, but why do I have to do that? 可以手动关闭它,但是为什么我必须这样做? It seems that when user closes the main form, the FormClosing event of the member form is fired with a parameter FormClosingEventArgs passed in and the CloseReason is the same as the CloseReason of the main form (which is UserClosing). 似乎当用户关闭主窗体时,使用传入的参数FormClosingEventArgs触发成员窗体的FormClosing事件,并且CloseReason与主窗体的CloseReason相同(即UserClosing)。 I don't understand this, I thought the CloseReason of the form is UserClosing only when user clicks on the X button, I thought the CloseReason for my member form is something like "MainFormClosing". 我不明白这一点,我认为仅当用户单击X按钮时,表单的CloseReason才是UserClosing,我认为我的会员表单的CloseReason类似于“ MainFormClosing”。

Is there some way to close the member form automatically as by default? 有没有默认方式自动关闭会员表单的方法?

UPDATE 更新

Here is the method showing the member form (showing it as a dialog): 这是显示成员表单的方法(将其显示为对话框):

private void ShowMemberForm_Click(object sender, EventArgs e){
     memberForm.ShowDialog();
}

But I don't think this matters, because when I load my main form, even I don't need to click on the ShowMemberForm button, and try closing my main form first, it doesn't still close the member form. 但是我认为这并不重要,因为当我加载主窗体时,即使我不需要单击ShowMemberForm按钮并尝试先关闭主窗体,它也不会关闭成员窗体。 Thanks! 谢谢!

UPDATE 更新

There is something strange here, I've tried commenting out the line e.Cancel = true , or even all the FormClosing event handler and the problem is still there. 这里有些奇怪,我尝试注释掉e.Cancel = true ,甚至是所有FormClosing事件处理程序,问题仍然存在。 This is so strange, it works OK before, I've just added the member form and this form relates to some Thread handling, but the thread starts only when a button on the member form is clicked. 这太奇怪了,之前可以正常工作,我刚刚添加了成员​​表单,并且该表单与某些线程处理有关,但是仅当单击成员表单上的按钮时,线程才会启动。 I didn't click that button. 我没有点击那个按钮。

What I have done in the past is set a flag when programatically closing 以编程方式关闭时,我过去所做的事情是设置一个标志

so in the MemberForm 所以在MemberForm中

private bool _ForceClose = false; 
public void ForceClose()
{
    _ForceClose = true; 
    this.Close(); 
}

private void MemberForm_FormClosing(object sender, FormClosingEventArgs e) 
{
   if(!_ForceClose)
   {
       e.Cancel = true;
       Hide();
   }
}

Then in your MainForm you can call 然后在您的MainForm中,您可以调用

memberForm.ForceClose(); 

From within your MainForms FormClosing method or from your MainForms Dispose() or Deconstructor. 从您的MainForms FormClosing方法内部,或者从您的MainForms Dispose()或Deconstructor。 It's low tech, but it works. 它的技术含量低,但是行得通。 Im not sure if you should put _ForceClose = true in your MemberForm's Dispose method, i'm fairly certain when it gets there its already been closed BUT it couldn't really hurt. 我不确定您是否应该在MemberForm的Dispose方法中放置_ForceClose = true,我很确定何时到达该位置时它已经关闭了,但并没有真正造成伤害。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM