0

我的代码运行良好,除非我添加了我不想要的大于或小于数字。如何修改我的代码,以便不需要的号码不会出现在列表框中。同样在我的阵列上,我认为 (10,100) 只会接受数字 10 到 100。

Private Sub GoBtn_Click(sender As Object, e As EventArgs) Handles GoBtn.Click

    If GotxtBox.Text < 10 Then
        MessageBox.Show("Can not be less than 10")
    End If
    If GotxtBox.Text > 100 Then
        MessageBox.Show("Can not be grater than 100 ")
    End If

    Dim number As Integer = Val(GotxtBox.Text) ' get number
    ' add the number to the end of the numberListBox
    GoLstBox.Items.Add(number)

    If ArrayCountInteger(10, 100) = 10 Then 'only allows  10 numbers but not necessarly 10-100???
        MessageBox.Show("You have entered the maximum number of items")
        Return
    End If
    numberArray(ArrayCountInteger(10, 100)) = GotxtBox.Text
    ArrayCountInteger(10, 100) += 1
    GotxtBox.Clear()
    GotxtBox.Focus()
End Sub
4

2 回答 2

0

我怀疑您希望您的代码更像以下内容,它检查输入是否可以转换为整数:

Dim arrayCount(99) As Integer
' (...other code...)

Private Sub GoBtn_Click(sender As Object, e As EventArgs) Handles GoBtn.Click

Dim nInput As Integer

' can we parse the input as an Integer?
If Not (Integer.TryParse(GotxtBox.Text, nInput)) Then
    MsgBox("I could not parse that as a whole number. Please try again.")
    Exit Sub
End If

If nInput < 10 Then
    MessageBox.Show("Can not be less than 10")
    Exit Sub
End If

If nInput > 100 Then
    MessageBox.Show("Can not be greater than 100")
    Exit Sub
End If

' add the number to the end of the numberListBox
GoLstBox.Items.Add(nInput.ToString())

If arrayCount(nInput) = 10 Then 'only allows 10 numbers
    MessageBox.Show("You have entered the maximum number of items.")
    Exit Sub
End If

arrayCount(nInput) += 1
GotxtBox.Clear()
GotxtBox.Focus()
End Sub
于 2013-10-01T21:48:03.673 回答
0

您可能需要返回(Exit Sub 可能在 vb am c# 背景中)

If GotxtBox.Text < 10 Then
    MessageBox.Show("Can not be less than 10")
    Exit Sub or Return'Am not sure about this(but return in c#)
End If
If GotxtBox.Text > 100 Then
    MessageBox.Show("Can not be grater than 100 ")
    Exit Sub or Return
End If
于 2013-10-01T21:26:21.563 回答