【问题标题】:Enter numeric value only into textbox in visual basic仅在 Visual Basic 中的文本框中输入数值
【发布时间】:2017-03-29 15:49:34
【问题描述】:

我正在尝试设置一个表单来接受电话号码,但我不确定如何验证它,所以它只会接受 11 位数字的值。

到目前为止,我一直在努力确保文本框中有内容

 'Validate data for Telephone Number
 If txtTelephoneNumber.Text = "" Then
 txtTelephoneNumber.Focus()
 MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Information)

【问题讨论】:

  • MaskedTextBox 控件就是为此而生的。

标签: vb.net


【解决方案1】:

我会暗示你正在使用 Windows 窗体。
把它写成你的 TextBox 的 Key Pressed 事件。

Private Sub myTxtBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles myTxtBox.KeyPress
If txtTelephoneNumber.Text.Length > 11 Then
   e.Handled= True
   return
End If
If Asc(e.KeyChar) <> 8 Then
    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
          e.Handled = True
    End If
End If
End Sub

应该(没有时间测试它)阻止用户输入任何不是数字的东西。它应该也可以防止他输入超过 11 个数字。

【讨论】:

  • 需要做If txtTelephoneNumber.Text.Length > 11。否则,首先处理KeyPress。
【解决方案2】:

在您的 textbox keydown 活动中。使用以下内容:

If Not IsNumeric(Chr(e.KeyCode)) Then
    e.SuppressKeyPress = True
End If

如果你想允许其他字符,你可以这样做:

If Not IsNumeric(Chr(e.KeyCode)) And Not e.KeyCode = 8 And Not e.KeyCode = 46 Then
    e.SuppressKeyPress = True
End If

'(8 = backspace key and 46 = Delete key)

【讨论】:

    【解决方案3】:

    你可以试试

    If txtTelephoneNumber.Text = "" Or Not IsNumeric(txtTelephoneNumber.Text) Or txtTelephoneNumber.Text.Length <> 11 Then
        txtTelephoneNumber.Focus()
        MessageBox.Show("You must enter a Telephone Number.", "Data Entry Error",      MessageBoxButtons.OK, MessageBoxIcon.Information)
    

    【讨论】:

      【解决方案4】:

      把它放在 TextBox 的 KeyPress 事件中

          'makes sure that only numbers and the backspace are allowed in the fields.
          Dim allowedChars As String = "0123456789" & vbBack
      
          'get a reference to the text box that fired this event
          Dim tText As TextBox
          tText = CType(sender, TextBox)
      
          If allowedChars.IndexOf(e.KeyChar) = -1 Then
              ' Invalid Character
              e.Handled = True
          End If
      
          if tText.Text.Length >= 11 then
              e.Handled = True
          End If
      

      【讨论】:

        【解决方案5】:

        我将它用于按键事件

        If e.KeyChar < CStr(0) Or e.KeyChar > CStr(9) Then e.Handled = True
        

        实际上,这看起来与我记得使用的不同,但它有效。虽然您也需要允许退格。

        或者我猜会更短

        If Not IsNumeric(e.KeyChar) Then e.Handled = True
        

        还有按键事件。

        您可以使用 MaxLength 设置文本框的最大长度

        【讨论】:

          【解决方案6】:

          将最大长度设置为 12 将其放入 text1 的按键中,它将用破折号和数字格式化它

              If Len(Text1.Text) = 3 Or Len(Text1.Text) = 7 Then
                  Text1.Text = Text1.Text & "-"
                  Text1.SelStart = Len(Text1.Text)
              End If
              If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
                  If IsNumeric(Chr(KeyAscii)) = False Then
                      KeyAscii = 0
                  End If
              ElseIf KeyAscii = 8 Then
                  If Right(Text1.Text, 1) = "-" Then
                      Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
                      Text1.SelStart = Len(Text1.Text)
                  End If
              End If
          

          如果您只想要数字文本框,请在按键中使用以下内容

              If KeyAscii <> 127 And KeyAscii <> 8 And KeyAscii <> 13 Then
                  If IsNumeric(Chr(KeyAscii)) = False Then
                      KeyAscii = 0
                  End If
              End If
          

          【讨论】:

            【解决方案7】:

            你可以使用 lostfocus 事件,在这个事件中你可以这样写:

            if not isnumeric(textbox1.text) then
               textbox1.gotfocus   'this part is to validate only numbers in the texbox and not let
                                    ' him go from the texbox
            endif
            

            你应该用 11 个字符定义文本框的最大长度

            【讨论】:

              猜你喜欢
              • 2014-03-20
              • 1970-01-01
              • 2015-10-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多