Use the If statements inside one another.
If 1 > 0 Then
If 2 < 3 Then
If 4 = 4 Then
MsgBox("You Win! :)", MsgBoxStyle.Information)
Else
MsgBox("error", MsgBoxStyle.Critical)
End If
Else
MsgBox("You Tie! :/", MsgBoxStyle.Information)
End If
Else
MsgBox("You Loose! :(", MsgBoxStyle.Information)
End If
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
Since you are new to this, a small hint:
If you get lost with the nested IFs and can't tell where each else (or end if) goes to which if, it might help you to add a comment at the end of the line starting the if with a number. Use the same number for the matching end if and you know where each if starts and ends.
Like this:
if 1=1 ' 1
if 2=2 ' 2
if 3=3 '3
...
end if '3
else ' 2
if a=a ' 4
else '4
end if '4
end if '2
else '1
end if ' 1
adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
I think correct indenting is easier to understand than the numbers beside them and is a habit you should get into. Other coders will expect to see it.
hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
I think that you are having a logical error here.
You are checking if the input is smaller or greater than the lblrandom and if it is, you check if it is equal to. If it is smaller or greater it can't be equal to. The if checking for equal to should be at the else clause of
If (Val(txtInput.Text) < Val(lblRandom.Text) Or Val(txtInput.Text) > Val(lblRandom.Text)) Then
PS: Can you give us a clear rule as to when somebody is winning or losing or draw? this is not clear from your code.
PS2: Does the lblRandom have a value from a previous run? And why are you setting it's value at that point?
adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
See if this will work for you (might contain errors, I just wrote it in the reply box)
If (Val(txtInput.Text) >= 0 And Val(txtInput.Text) <= 9) Then
If (Val(txtInput.Text) < Val(lblRandom.Text) and Val(txtInput.Text) > Val(lblRandom.Text)) Then
If (Val(txtInput.Text) < Val(lblRandom.Text)) Then
lblOutput.Text = "You Lose!"
Else
lblOutput.Text = "You Win!"
End If
Else
lblOutput.Text = "Draw!"
End If
This could be a valuable lesson, so make sure you understand how each if affects the other, and why my code has a chance to work.
adam_k
Practically a Posting Shark
803 posts since Jun 2011
Reputation Points: 256
Solved Threads: 149
See if this helps.
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'// CInt="Convert To Integer".
If CInt(txtInput.Text) >= 0 AndAlso CInt(txtInput.Text) <= 9 Then '// if correct # entered, proceed.
Dim rnd As New Random '// declare new random.
lblRandom.Text = rnd.Next(0, 10).ToString '// Randomize (0 is minimum #, 10 is max and will only randomize to 9)
If CInt(txtInput.Text) > CInt(lblRandom.Text) Then lblOutput.Text = "You Win!"
If CInt(txtInput.Text) < CInt(lblRandom.Text) Then lblOutput.Text = "You Lose!"
If CInt(txtInput.Text) = CInt(lblRandom.Text) Then lblOutput.Text = "Draw!"
Else
MsgBox("Number entered can only be between 0 and 9.", MsgBoxStyle.Critical)
txtInput.Select()
txtInput.SelectAll()
End If
End Sub
'// Numeric TextBox.
Private Sub txtInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInput.KeyPress
Select Case e.KeyChar
Case "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "0"c, CChar(vbBack) '// allow numeric and backspace.
Exit Sub
Case Else
e.Handled = True '// block.
End Select
End Sub
End Class
codeorder
Posting Virtuoso
1,915 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384