3

我有一个消息框,当我按下一个基本上说“你确定要退出”的关闭按钮时会弹出一个消息框,但是当我单击否按钮或取消但程序以任何方式关闭时

这是我的代码:

'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
4

8 回答 8

5

你对 . 的值什么也不做result。您需要检查该值并确定您是否调用 Me.Close()。代码大约

If result = DialogResult.Yes Then
    Me.Close()
End If
于 2013-06-26T02:45:15.353 回答
3

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

尝试使用 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
于 2013-06-26T05:06:09.277 回答
2

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

于 2013-06-26T02:42:59.123 回答
2
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
于 2013-06-26T02:45:57.063 回答
2

复制这个:

    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
于 2016-08-16T16:34:37.807 回答
1
 Dim result = MessageBox.Show(" Are you sure you want to quit", "System Reminder", MessageBoxButtons.YesNo)
    If result = DialogResult.Yes Then
        Me.Close()

    End If
于 2014-02-17T06:12:44.227 回答
1

如果它是子窗体,它会作为主窗体中的按钮的结果打开:

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 
于 2018-05-29T05:05:56.913 回答
0

您可以使用以下代码:

Dim closingfrm = MsgBox(" Are you sure to close", MsgBoxStyle.YesNo)
If closingfrm = DialogResult.Yes Then
Application.Exit()
End If
于 2018-10-28T07:50:33.250 回答