vb.net 我在哪里可以控制 winform 右上角的“X”关闭按钮的行为?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2838979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:52:49  来源:igfitidea点击:

Where do I control the behavior of the "X" close button in the upper right of a winform?

vb.netwinformsvisual-studio-2005modeless

提问by John

I'm venturing into making my VB.NET application a little better to use by making some of the forms modeless.

我正在冒险通过使一些表单无模式来使我的 VB.NET 应用程序更易于使用。

I think I've figured out how to use dlg.Show() and dlg.Hide() instead of calling dlg.ShowDialog(). I have an instance of my modeless dialog in my main application form:

我想我已经弄清楚如何使用 dlg.Show() 和 dlg.Hide() 而不是调用 dlg.ShowDialog()。我的主应用程序表单中有一个无模式对话框的实例:

Public theModelessDialog As New dlgModeless

To fire up the modeless dialog I call

为了启动我调用的无模式对话框

theModelessDialog.Show()

and within the OK and Cancel button handlers in dlgModelessI have

在确定和取消按钮处理程序中,dlgModeless我有

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Hide()
End Sub

Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.Hide()
End Sub

and that seems to work fine.

这似乎工作正常。

The "X" button in the upper right is getting me, though. When I close the form with that button, then try to reopen the form, I get

不过,右上角的“X”按钮正在吸引我。当我用那个按钮关闭表单,然后尝试重新打开表单时,我得到

ObjectDisposedException was unhandled. Cannot access a disposed object.

ObjectDisposedException 未处理。无法访问已处置的对象。

I feel like I'm most of the way there but I can't figure out how to do either of the following:

我觉得我大部分时间都在那里,但我无法弄清楚如何执行以下任一操作:

  • Hide that "X" button
  • Catch the event so I don't dispose of the object (just treat it like I hit Cancel)
  • 隐藏那个“X”按钮
  • 捕捉事件,所以我不会处理对象(就像我点击取消一样对待它)

Any ideas?

有任何想法吗?

The class of this dialog is System.Windows.Forms.Form.

这个对话框的类是System.Windows.Forms.Form.

采纳答案by Matthew Scharley

Use Me.Close()to hide the form. To open it, use the following snippet:

使用Me.Close()隐藏表单。要打开它,请使用以下代码段:

If theModelessDialog.IsDisposed Then
    theModelessDialog = New dlgModeless
End If
dlgModeless.Show()

If this is saving data, then you'll need to figure some way of storing it (perhaps in a static variable/s in the form). This is the proper way to do what you are trying to achieve though.

如果这是保存数据,那么您需要想办法存储它(也许在表单中的静态变量中)。这是做你想要实现的目标的正确方法。

You'll also have to forgive me if my VB is off, it's been a while.

如果我的 VB 关闭了,你也必须原谅我,已经有一段时间了。

回答by Michael Todd

Catch the FormClosing event and, if the reason is UserClosing, set Cancel on the event to true.

捕获 FormClosing 事件,如果原因是 UserClosing,则将事件的 Cancel 设置为 true。

Something like the following:

类似于以下内容:

Private Sub Form1_FormClosing(sender as Object, e as FormClosingEventArgs) _ 
     Handles Form1.FormClosing

    if e.CloseReason = CloseReason.UserClosing then
        e.Cancel = true
        Me.Hide()
    end if

End Sub

回答by Philip

the formclosing event allows me to do a managed exit of the form so I have included a question to confirm to exit. I also have a form flag bterminate to force the cancel where i want it to and therefore not ask the question. Thanks your suggestion helped me as well :)

表单关闭事件允许我对表单进行托管退出,因此我包含了一个问题以确认退出。我还有一个表单标志 bterminate 来强制取消我想要的地方,因此不问这个问题。谢谢你的建议也帮助了我:)

    Dim msgboxresponse As MsgBoxResult

    If e.CloseReason = CloseReason.UserClosing Then
        If Not Me.bTerminate Then
            msgboxresponse = MsgBox("Are you sure you want to cancel adding?", _
                                MsgBoxStyle.Question + MsgBoxStyle.YesNo, Me.Text)
            If msgboxresponse <> MsgBoxResult.Yes Then
                e.Cancel = True
                Return
            End If
        End If
    End If

回答by Neil Dunlop

@Johnwas Hiding the form in his code and the answers above provide a solution to that case. Often, though, you are not planning to use the form again, so you really dowant the form to be Disposed. All Close related activities will be in one place if you Handle the FormClosing event using Me.FormClosingby adding it to anyCancel/Close/Exit code that you already have. e.g. in @John's case:

@John在他的代码中隐藏了表单,上面的答案为这种情况提供了解决方案。不过,通常来说,你不打算再次使用的形式,让您真正做到想要的形式来处置。如果您Me.FormClosing通过将FormClosing 事件添加到您已有的任何取消/关闭/退出代码来处理 FormClosing 事件,则所有与关闭相关的活动都将位于一处。例如在@John 的案例中:

Private Sub Cancel_Button_Click(ByVal sender As System.Object, _ 
                                     ByVal e As System.EventArgs) _
                                 Handles Cancel_Button.Click, Me.FormClosing
....More code
Me.Dispose
End Sub

Note the use of the Me.Disposeinstead of any existing Me.Close. If you leave the Me.Closeyou'll create an infinite loop. See this for the subtle differences between Close and Dispose.

请注意使用Me.Dispose代替任何现有的Me.Close。如果你离开Me.Close你会创建一个无限循环。请参阅此处了解 Close 和 Dispose 之间的细微差别

回答by Esther Hearne

I've tried everything and it didn't work if you just want to close, without showing an messagebox, you will just need:

我已经尝试了所有方法,但如果您只想关闭而不显示消息框,则它不起作用,您只需要:

Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) 处理我.FormClosing

 >e.Cancel = False
 >FORM2.Show()   (if you want to show another form)

End Sub

结束子

Hope this helps you...!

希望这对你有帮助...!

回答by Robert McBean

Agree with handling the FormClosing event. Or change the properties on the form to hide the system X control.

同意处理 FormClosing 事件。或者更改窗体上的属性以隐藏系统 X 控件。