我的代码效果很好,除非我添加一个我不想要的超过或低于数字。如何修改我的代码,以便不需要的数字不会显示在列表框中。同样在我的阵列上,我认为(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
答案 0 :(得分:0)
您可能需要返回(退出Sub可能在vb am c#background)
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
答案 1 :(得分:0)
我怀疑您希望您的代码更像下面的代码,它会检查输入是否可以转换为Integer:
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