简体   繁体   English

Visual Basic MsgBox退出

[英]Visual Basic MsgBox exiting

I created a message box which asks the user if he/she wants to close the application. 我创建了一个消息框,询问用户是否要关闭该应用程序。

I came up with this class: 我上了这堂课:

Private Sub closeAll_Click(sender As Object, e As EventArgs) Handles closeAll.Click
        MsgBox("Do you want to terminate the program?", MsgBoxStyle.YesNo, "Close?")
        If MsgBoxResult.Yes Then
            Application.Exit()
        End If
    End Sub

If I select "Yes", it works fine as it exits the application successfully. 如果我选择“是”,则它在成功退出应用程序时可以正常工作。 But if "No" is selected, it would still close. 但是,如果选择“否”,它将仍然关闭。 Does it really need an "Else" statement to do so? 确实需要“ Else”语句吗?

If it does, I don't know the proper coding for letting the program "not do something". 如果是这样,我不知道让程序“不做某事”的正确编码。

Can someone help? 有人可以帮忙吗?

MsgBox() is a function that returns the result ( MsgBoxResult enum) so your code should be: MsgBox()是一个返回结果的函数( MsgBoxResult枚举),因此您的代码应为:

Private Sub closeAll_Click(sender As Object, e As EventArgs) Handles closeAll.Click
    If MsgBox("Do you want to terminate the program?", MsgBoxStyle.YesNo, "Close?") = MsgBoxResult.Yes Then
        Application.Exit()
    End If
End Sub

The way you wrote is you take the value of enum MsgBoxResult.Yes and check if it's true. 编写方法是采用枚举MsgBoxResult.Yes的值,并检查其是否为真。 That causes an implicit conversion to a boolean value which is true since the enum value is not zero. 由于枚举值不为零,这将导致隐式转换为布尔值,该值为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM