Reputation: 15
Private Sub btnConfirmInfantry_Click(sender As Object, e As EventArgs) Handles btnConfirmInfantry.Click
Dim input = InputBox("How many do you want to attack with?", "Choose how many Infantry:")
Dim infantry As Integer
If (input > frmMainGame.lblInfantryNumberPlayer.Text) Then
MessageBox.Show("Error: The inputted number has to be <= current number of infantry.")
Else
If Integer.TryParse(input, infantry) Then
Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture)
frmMainGame.lblHPAI.Text = String.Format("{0:n0}", hpai - infantry * 2)
Dim numPlayer = frmMainGame.lblInfantryNumberPlayer.Text 'Integer.Parse(frmMainGame.lblInfantryNumberPlayer.Text)
frmMainGame.lblInfantryNumberPlayer.Text = (numPlayer - input).ToString("N0")
End If
End If
End Sub
I am attempting to get the If (input > frmMainGame.lblInfantryNumberPlayer.Text) Then Skip the rest of the line of code. But, the issue is that the lblInfantry numbers are in the millions and it is only reading the first number which is 2. So if I input 3, the error msgbox will occur. I have tried the Globilization.NumberStyles.AllowThousands by putting it after the frm.... but that did not work. Any suggestions? Thanks!
Note: I want to put a input value of like 30 in and if the lblInfantry is like 100,000 then the msgbox will NOT occur and the code will read after the Else.
Update:
Private Sub btnConfirmInfantry_Click(sender As Object, e As EventArgs) Handles btnConfirmInfantry.Click
Dim input = InputBox("How many do you want to attack with?", "Choose how many Infantry:")
Dim infantry As Integer
Dim intInfanty As Integer = 0
Dim intInput As Integer = 0
Dim txt As String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry
If Integer.TryParse(input, intInput) AndAlso Integer.TryParse(txt, intInfantry) AndAlso (intInput > intInfantry) Then
If (IsNumeric(input) AndAlso IsNumeric(frmMainGame.lblInfantryNumberPlayer.Text) AndAlso CInt(input) > CInt(frmMainGame.lblInfantryNumberPlayer.Text)) Then
MessageBox.Show("The value can not be a letter.")
Else
If Integer.TryParse(input, infantry) Then
Dim hpai = Integer.Parse(frmMainGame.lblHPAI.Text, Globalization.NumberStyles.AllowThousands, Globalization.CultureInfo.InvariantCulture)
frmMainGame.lblHPAI.Text = String.Format("{0:n0}", hpai - infantry * 2)
Dim numPlayer = frmMainGame.lblInfantryNumberPlayer.Text 'Integer.Parse(frmMainGame.lblInfantryNumberPlayer.Text)
frmMainGame.lblInfantryNumberPlayer.Text = (numPlayer - input).ToString("N0")
End If
End If
Else
MessageBox.Show("Error: The inputted number has to be <= current number of infantry.")
End If
End Sub
Upvotes: 0
Views: 55
Reputation: 21672
You need to convert your input values to integers. Using a comparative operator like >
on strings will do the comparison alphabetically, so for example, "20000" < "3"
returns true
- alphabetically, 20000
comes first.
Dim intInfanty as int = 0;
Dim intInput as int = 0;
Dim txt as String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry;
If Int.TryParse(input, intInput) AndAlso Int.TryParse(txt, intInfantry) Then
If (intInput > intInfantry) Then
'Input > InfantryNumberPlayer
Else
'Input <= InfantryNumberPlayer
End If
Else
'Skipped the > comparison because one of them wasn't a number
'"Please make sure you entered a valid number"
End If
If you want to combine the If
statements so that non-numeric values fall into the same else
as the >
comparison, you can do this:
Dim intInfanty as int = 0;
Dim intInput as int = 0;
Dim txt as String = frmMainGame.lblInfantryNumberPlayer.Text, intInfantry;
If Int.TryParse(input, intInput) AndAlso Int.TryParse(txt, intInfantry) AndAlso (intInput > intInfantry) Then
'Input > InfantryNumberPlayer
Else
'Anything else happened
'(Input was <= InfantryNumberPlayer, OR one wasn't a number)
End If
Upvotes: 1