Reputation: 1
My Windows Form Application has a Log-out button that when clicked, prompts a MsgBox to pop-up asking if the user is sure he wants to log-out or not. I'm new to Visual Basic so I'm not sure what I'm supposed to put in the Elseif statement so that when the user clicks "No" on the MsgBox, the MsgBox will be gone and the Form will be showed again. Here's my code:
Private Sub cmd_logout_Click(sender As Object, e As EventArgs) Handles cmd_logout.Click
MsgBox("Are you sure you want to log-out?", MsgBoxStyle.YesNo)
If MsgBoxResult.Yes Then
Me.Close()
ElseIf MsgBoxResult.No Then
(I'm not sure what I should put here I'm having a hard time figuring it out)
End If
End Sub
Thanks in advance.
Upvotes: 0
Views: 49
Reputation: 71187
MsgBox
is a function, it returns a MsgBoxResult
enum value.
You're using it as a procedure, discarding the return value, and then testing conditions with constant expressions.
Might as well do this:
If 6 Then
ElseIf 7 Then
End If
Or whatever the MsgBoxResult.Yes
and MsgBoxResult.No
enum values stand for. Obviously that doesn't make much sense.
The solution is to capture the function's return value into a variable:
Dim result As MsgBoxResult = MsgBox("blablabla", MsgBoxStyle.YesNo)
And then to test that result against the constants:
If result = MsgBoxResult.Yes Then
That said, MsgBox
is the legacy way of doing things, included in Microsoft.VisualBasic
to facilitate transition from VB6/VBA.
The idiomatic .NET way is to use MessageBox.Show
instead.
Also, specify Option Strict On
at the top of the module (or better: at project level) to have the compiler warn you about undeclared variables and implicit type conversions, resulting in more solid and .NET-idiomatic (less VB6-ish) VB code.
Upvotes: 3