user15338372
user15338372

Reputation:

Visual Basic MsgBox vbYesNo Doesn't accept when the 'No' button is clicked

I have a MsgBox in my VB.NET Form's program with a vbYesNo parameter attached. Whenever the messagebox shows, I see the yes and no buttons, however no matter which button I click, I always get the same result.

Here's my code:

MsgBox("Here's 2 options...", vbYesNo)
If vbYes Then
    MsgBox("You clicked yes")
Else
    MsgBox("You clicked no")
End If

I've also tried the same as above where the If statement is:

If vbYes Then
    MsgBox("You clicked yes")
ElseIf vbNo Then
    MsgBox("You clicked no")
End If

When I click Yes, or No on the Messagee Box, I always get whatever's in:

If vbYes Then
    MsgBox("You clicked yes")

I'm relatively new to Visual Basic forms so I'm sorry if this is a simple rookie mistake. I hope this info is enough to go on.

Thanks for the help.

Upvotes: 0

Views: 682

Answers (2)

It all makes cents
It all makes cents

Reputation: 4993

Try the following:

Option 1:

If MessageBox.Show("Here's 2 options", "Select Option", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    MessageBox.Show("You clicked yes", "Result")
Else
    MessageBox.Show("You clicked no", "Result")
End If

Option 2:

If MsgBox("Here's 2 options", vbYesNo, "Select option") = vbYes Then
    MsgBox("You clicked yes", vbOKOnly, "Result")
Else
    MsgBox("You clicked no", vbOKOnly, "Result")
End If

Upvotes: 1

Misael Moneró
Misael Moneró

Reputation: 869

your issue is that you are not capturing the value returned by the MsgBox, you should try something like this:

   Dim a As MsgBoxResult = MsgBox("Here's 2 options...", vbYesNo)
    If a = vbYes Then
        MsgBox("You clicked yes")
    Else
        MsgBox("You clicked no")
    End If

Upvotes: 1

Related Questions