Scody
Scody

Reputation: 1

Using multiple Message Boxes in "if then" statement with VB

I am very new with programming and I have come across an issue in Visual Basic that I cannot figure out. Many forums and YouTube videos later I still do not have an answer. I am using a nested selection structure and within it is two Message boxes. I cannot figure out how to get the second dialog result to trigger the elseif statement. It just skips over it. I believe since I have one variable declared for a dialog result it is checking both of them, but in this case I don't know how to declare only the second dialog result. Here is the code so far.

    Dim dblTotal As Double = 12
    Dim strResponse As DialogResult

' Dialog box asking about a coupon and $2 coupon. If MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo) = vbYes AndAlso

MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo) = vbNo Then

lblTotal.Text = Convert.ToString(dblTotal - 4)

        ' Meant to be ran if statement is false. I dont Understand
        ' why it is skipping over and not executing. 
        ' Is "dlgResult" reading the first one as well? How do I correct?

    ElseIf strResponse = vbYes Then

        lblTotal.Text = Convert.ToString(dblTotal - 2)

    Else

        lblTotal.Text = Convert.ToString(dblTotal)
    End If

End Sub

I understand it would be easier to to code if the first message = vbNo, but I was trying to see if this way would work. Thank you!!

Upvotes: 0

Views: 216

Answers (1)

pokeymond
pokeymond

Reputation: 671

Is this how you wanted it?

Dim dialog1 As DialogResult
Dim dialog2 As DialogResult
Dim dblTotal As Double = 12

dialog1  = MessageBox.Show("Does customer have a coupon?", "Coupon", MessageBoxButtons.YesNo)
dialog2 = MessageBox.Show("Does customer have a $2 coupon?", "Coupon", MessageBoxButtons.YesNo) 

If dialog1 = DialogResult.OK Then
   dblTotal = dblTotal - 2
End If

If dialog2 = DialogResult.OK Then
   dblTotal = dblTotal - 2
End If

lblTotal.Text = Convert.ToString(dblTotal - 2)

Upvotes: 1

Related Questions