简体   繁体   中英

Closing choice via Dialog VB 2010

The following code is a what I have done in order to show a MsgBox when an exit ITEM on a MenuStrip is clicked then it will give two different choices Yes and No. If Yes then it will close the application but if No then it should stay on the same page.

There are no errors shown but it neither of the buttons are doing anything. Please get back to me on this one.

Thanks in Advance!

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles ExitMenu.Click
    MsgBox("Are you sure you want to exit the program?", MsgBoxStyle.YesNo)
    If MsgBoxResult.Yes = True Then
        Application.Exit()
    End If

If you display a messagebox in vb.net you call a function that returns the result and you then compare it to the MsgBoxResult Enum

Change your code so that it looks like this:

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitMenu.Click 
If MsgBox("Are you sure you want to exit the program?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then 
    Application.Exit() 
End If 

You can also compare it to the integer value of the MsgBoxResult enum which is 6 for yes:

If MsgBox("Are you sure you want to exit the program?", MsgBoxStyle.YesNo) = 6

try this

 If MsgBox("Are you sure you want to exit the program?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
        Application.Exit()
    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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