vb.net 使用消息框确认程序关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17310695/
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 16:57:57  来源:igfitidea点击:

Confirming program close with a messagebox

vb.net

提问by Binglee2323

I have a message box that pops up when i press a close button that basicaly says" Are you sure you want to quit" but when i click the no button or cancel but the program closes any how

我有一个消息框,当我按下一个关闭按钮时会弹出一个消息框,上面写着“您确定要退出吗”但是当我单击“否”按钮或取消但程序以任何方式关闭时

this is my code:

这是我的代码:

'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click

    Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel)
    Me.Close()

End Sub

回答by billinkc

You are doing nothing with the value of result. You need to inspect the value and determine whether you call Me.Close(). Code approximately

您对 的价值无动于衷result。您需要检查该值并确定您是否调用了 Me.Close()。代码大约

If result = DialogResult.Yes Then
    Me.Close()
End If

回答by bansi

If you are using then message box to prevent accidental form close, your approach may not work always. The message box will not be shown if the user closes the application in any other way than clicking the "Close" button.

如果您使用 then 消息框来防止意外关闭表单,您的方法可能并不总是有效。如果用户以单击“关闭”按钮以外的任何其他方式关闭应用程序,则不会显示消息框。

Try using the FormClosing event.

尝试使用 FormClosing 事件。

'Close Button
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Me.Close()
End Sub

'FormClosing Event
Private Sub MyForm_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel) <> DialogResult.Yes
         e.Cancel = True
    End If
End Sub

回答by Johannes Kawali

Copy this:

复制这个:

    Dim result = MessageBox.Show(" Are you sure you want to end the Application", "School Management System", MessageBoxButtons.YesNoCancel)
    If result = DialogResult.Yes Then
        Me.Close()
    End If

回答by SSS

Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
  If MsgBox("Are you sure you want to quit?", MsgBoxStyle.YesNo Or MsgBoxStyle.DefaultButton2, "Close application") = Windows.Forms.DialogResult.Yes Then
    Me.Close()
  End If
End Sub

回答by Raptor

You issue Me.Close()no matter what the resultis. Check the result and execute Me.Close()only the user clicks Yes

Me.Close()不管是什么,你都发出result来。检查结果并Me.Close()仅执行用户点击Yes

回答by Dr. Ugs Production

 Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        Me.Close()

    End If

回答by Ali

If it is a child form, it opens as a result of a button in a main form:

如果它是子窗体,它会通过主窗体中的按钮打开:

If MessageBox.Show(" Are you sure you want to exit the application ? ", "Exit  ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then

 Me.Hide() : MainForm.Show()

        Else
            e.Cancel = True
        End If 

回答by Yakub Ahmed Yakub

You can use the following code:

您可以使用以下代码:

Dim closingfrm = MsgBox(" Are you sure to close", MsgBoxStyle.YesNo)
If closingfrm = DialogResult.Yes Then
Application.Exit()
End If