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

Vb.net 用消息框确认程序关闭,vb.net,Vb.net,我有一个消息框,当我按下一个关闭按钮时弹出,Basicali会说“你确定要退出吗”,但当我单击“否”按钮或“取消”但程序无论如何关闭时,会弹出一个消息框 这是我的代码: 'Close Button Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click Dim result = MessageBox.Show(" Are y

我有一个消息框,当我按下一个关闭按钮时弹出,Basicali会说“你确定要退出吗”,但当我单击“否”按钮或“取消”但程序无论如何关闭时,会弹出一个消息框

这是我的代码:

'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

您可以发出
Me.Close()
,无论
结果如何。检查结果并执行
Me.Close()
只有用户单击
Yes

您对
result
的值不做任何操作。您需要检查该值并确定是否调用Me.Close()。近似编码

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

如果您正在使用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
“关闭”按钮
私有子BtnClose_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理BtnClose。单击
我
端接头
'关闭事件
私有子MyForm_Closing(ByVal sender作为System.Object,ByVal e作为System.Windows.Forms.FormClosingEventArgs)处理Me.FormClosing
如果MessageBox.Show(“您确定要退出吗”,“确定吗?”,MessageBox按钮.YesNoCancel)对话框结果。是
e、 取消=真
如果结束
端接头
复制以下内容:

    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

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

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 

您可以使用以下代码:

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

您从未测试过的结果可能重复,请重新格式化代码,使其全部位于代码块内,并正确缩进。有关更多信息,请参阅。
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 
Dim closingfrm = MsgBox(" Are you sure to close", MsgBoxStyle.YesNo)
If closingfrm = DialogResult.Yes Then
Application.Exit()
End If