0

我将如何确保当用户在输入框中输入他的“组合”时,它必须至少有 5 个字符?

这是我到目前为止的代码:

If (tboxStatus.Text) = "Combination Not Set" Or (tboxStatus.Text) = "UnLocked" Then
  Combination = CInt(InputBox("Set the Combination First"))
  tboxStatus.Text = "Locked"
ElseIf (tboxStatus.Text) = "Locked" Then
  MsgBox("You must first UnLock the safe before trying to change the combination.")
End If
4

2 回答 2

1

对于初学者...

Dim value as String = InputBox("Set the Combination First")
If (value.Trim.Length < 5) Then
    MsgBox ("Combination must be at least 5 characters")
Else
    Combination = CInt(value)
End If

除其他外,您需要在执行 CInt() 之前检查它是否为数字

于 2013-03-21T21:25:26.707 回答
0
If tboxStatus.Text = "Combination Not Set" OrElse tboxStatus.Text = "UnLocked" Then
    Dim result As String = ""

    While String.IsNullOrWhiteSpace(result) OrElse Not Integer.TryParse(result, Combination)
       result= InputBox("Set a Combination Number First")
    End While

    tboxStatus.Text = "Locked"

ElseIf tboxStatus.Text = "Locked" Then
    MsgBox("You must first UnLock the safe before trying to change the combination.")
End If
于 2013-03-21T21:34:31.713 回答