输入框值系统

时间:2016-10-28 20:30:39

标签: .net vb.net winforms

    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

我试图获取If(输入&gt; frmMainGame.lblInfantryNumberPlayer.Text)然后跳过剩下的代码行。 但是,问题在于lblInfantry数字是数百万,它只读取第一个数字是2.因此,如果我输入3,将发生错误msgbox。我已经尝试了将它放在frm之后的Globilization.NumberStyles.AllowThousands ....但是这不起作用。有什么建议?谢谢!

注意:我想输入一个类似于30的输入值,如果lblInfantry是100,000,那么msgbox将不会发生,代码将在Else之后读取。

更新

    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

1 个答案:

答案 0 :(得分:1)

您需要将输入值转换为整数。在字符串上使用>之类的比较运算符会按字母顺序进行比较,例如,"20000" < "3"会返回true - 按字母顺序排列,20000首先出现。

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语句,以便非数字值与else比较属于同一>,则可以执行以下操作:

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