如何一个WinForm中的FormClosing事件prevent结算及处理?事件、WinForm、FormClosing、prevent

2023-09-03 04:03:23 作者:数学滚回你的阿拉伯亡国吧

这问题似乎是一个重复的,但我只是跑这个问题,而我是测试我的节目,我就是你怎么解决呢样的困惑,。

This question may seem like a duplicate, but I just ran in to this issue while I was testing my program and I am kind of confused as to how you solve it.

我有一个WinForm,它有一个表单关闭事件。在活动现场,我打开弹出一个消息框,询问用户,您确定要关闭该窗口?如果他们把Yes按钮,该应用程序被设置为预期的关闭窗口,prevents它。所以,我可以再次打开它。然而,如果他们没有推按钮时,它仍关闭窗口,但现在的窗口布置。所以,当我试图再次打开它,它提出了一个例外,无法访问已释放的对象。当按下否按钮,我想在WinForm保持打开,而不是处理。

I have a winform and it has a form closing event. In the event, I pop open a messagebox asking the user, "Are you sure you want to close the window?." If they pushed yes button, the application closes window and prevents it from being disposed as expected. So, I can open it again. However, if they pushed no button, it still closes the window, but now the window is disposed. So, when I try to open it again, it raised an exception, "Cannot access a disposed Object." When No button is pushed, I want the winform remain opened and not disposed.

下面是我的code:

method PPMain.PPMain_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
       if MessageBox.Show('Are you sure you want to close the window?','PPMain',MessageBoxButtons.YesNo) = DialogResult.Yes then
       begin
             e.Cancel := true; 
             Hide; 
       end
       else
             e.Cancel := false;
end;

我想既然你要设置e.Cancel = true来关闭该窗口,并告诉它隐藏,这样做的(e.Cancel =虚假和不隐藏),相反会prevent从收盘在WinForm和布置。

I thought since you have to set e.Cancel = true to close the window and tell it to hide, doing the opposite of that (e.Cancel=false and no hiding) will prevent the winform from closing and being disposed.

你怎么解决这个问题?

感谢提前,

推荐答案

e.Cancel = TRUE $ P $从收盘pvents窗口 - 它停止关闭事件。

e.Cancel = true prevents the window from closing - it stops the close event.

e.Cancel = FALSE 允许关闭事件继续(导致窗口关闭和布置;假设没有别的停止它)。

e.Cancel = false allows the "close event" to continue (resulting in the window closing and being disposed; assuming nothing else stops it).

看来要做到这一点:

method PPMain.PPMain_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
      e.Cancel := true; 
      if MessageBox.Show('Are you sure you want to close the window?','PPMain',MessageBoxButtons.YesNo) = DialogResult.Yes then
      begin
            Hide; 
      end
end

e.Cancel:= TRUE; prevents从关闭窗口。然后向用户提示,如果他们说是隐藏; 隐藏窗口(无处理)。如果用户单击否,什么都不会发生。

e.Cancel := true; prevents the window from closing. The user is then prompted, if they say yes Hide; hides the window (without disposing). If the user clicks No, nothing happens.

有可能以检测什么样的接近动作正在执行一个好主意。使用 e.CloseReason 以免在操作系统关闭或类似的规定prevent结束。

It might be a good idea to detect what kind of close action is being performed. Using e.CloseReason so as not to prevent closing during OS shutdown or something along those lines.

这样的:

method PPMain.PPMain_FormClosing(sender: System.Object; e: System.Windows.Forms.FormClosingEventArgs);
begin
      if e.CloseReason = System.Windows.Forms.CloseReason.UserClosing then
      begin
           e.Cancel := true; 
           if MessageBox.Show('Are you sure you want to close the window?','PPMain',MessageBoxButtons.YesNo) = DialogResult.Yes then
           begin
                 Hide;
           end
      end
end