【问题标题】:Checking to see if text box input is numeric检查文本框输入是否为数字
【发布时间】:2013-03-15 01:19:37
【问题描述】:

我已经对此进行了一些研究,但仍然无法让我的程序运行。我只需要检查文本框以查看用户输入是否为数值(“。”和或“/”除外)

到目前为止我的代码,

 Private Sub Num1_KeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Num1.KeyPress
    Dim UserEntry As Boolean
    If UserEntry = IsNumeric(False) Then
        MessageBox.Show("That's not numeric!")
    End If
End Sub

【问题讨论】:

  • 对于像 3.45 这样的小数,它可以是像 1/2 这样的分数吗?
  • 是的,这就是为什么我需要排除字符“。”和比较中的“/”。对不起,我刚刚自学VB.NET,这样的过程对我来说还是新的。
  • 您可以使用开箱即用的具有此功能的 NumericUpDown1
  • 如果输入的文本是数字,而不是陷印。为什么不将文本框接受的字符限制为仅 0123456789./ 和 Backspace?
  • @Ruben 这会将3.4.5.6 视为数字。

标签: vb.net validation text numeric


【解决方案1】:

我建议处理 TextChanged 并检查整数而不是单个字符。

Private Sub Num1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles Num1.TextChanged
        If IsInputNumeric(Num1.Text) Then
            'handle numeric input
        Else
            'handle not a number
        End If
    End Sub

    Private Function IsInputNumeric(input As String) As Boolean
        If String.IsNullOrWhiteSpace(input) Then Return False
        If IsNumeric(input) Then Return True
        Dim parts() As String = input.Split("/"c)
        If parts.Length <> 2 Then Return False
        Return IsNumeric(parts(0)) AndAlso IsNumeric(parts(1))
    End Function

【讨论】:

  • 感谢特雷弗的回复!它比 AbZy 的更详细,我选择他作为这个(特定)问题的答案,但你的解决方案更准确,我感谢你抽出时间帮助我解决这个问题,因为你应该得到很多支持!
【解决方案2】:

我认为你最好使用TextBox.KeyUp 事件,它通过KeyEventArgs。试试这个:

Private Sub Num1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Num1.KeyUp

    Dim isDigit As Boolean = Char.IsDigit(ChrW(e.KeyValue))
    Dim isKeypadNum As Boolean = e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9
    Dim isBackOrSlashOrPeriod As Boolean = (e.KeyCode = Keys.Decimal Or e.KeyCode = Keys.Oem2 Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.OemPeriod)

    If Not (isDigit Or isKeypadNum Or isBackOrSlashOrPeriod) Then
        MessageBox.Show("That's not numeric!")
    End If

End Sub

【讨论】:

  • 这似乎不起作用,它只省略了文本框中第一个“空格”、字母或数字的消息框,然后为之后的每个输入提供消息框,无论是数字、字母、正斜杠或退格。
  • 你的函数运行良好!我知道的一个问题是键盘的遗漏?键盘是否属于不同的分类?因为当我通过键盘输入数值时也会出现消息框。
  • @Matt 我现在修好了。是的,它们有不同的键码。
【解决方案3】:
Public Function onlyNumbers(ByVal KeyChar As Char) As Boolean
    Dim allowedChars As String

    allowedChars = "0123456789./" 

    If allowedChars.IndexOf(KeyChar) = -1 And (Asc(KeyChar)) <> 8 Then 
        Return True
    End If

    Return False
End Function

true 表示无效字符。

在按键时你需要做的:

e.handled = onlyNumbers(e.keychar)

【讨论】:

  • 不是-2 number 吗? 2.2.2.2. 是一个数字吗?
【解决方案4】:

我发现这种验证更容易在文本框的LostFocus eventHandler 中进行,或者在表单级别,例如当用户点击确定按钮时。

然后您可以按如下方式进行验证

a) 文本框是否包含"0123456789./" 之外的任何字符如果是,则为非数字

b) 在出现"/" 字符的地方(如果有)拆分文本,然后对每个子字符串使用IsNumeric() 函数。如果其中任何一个不是数字,则文本不是数字。

这确实假设您允许 1/2/2,即1/4。如果不是,那么您还必须检查您的字符串中是否最多有 1 个“/”字符。

【讨论】:

    【解决方案5】:
    Private Sub tbYear_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbYear.KeyPress
        If e.KeyChar < Chr(48) Or e.KeyChar > Chr(57) Then
            e.KeyChar = Nothing
        End If
    End Sub
    

    【讨论】:

      【解决方案6】:
      ' Validates textboxes for numeric only keystrokes.  Hook this up to the 
      ' PreviewTextInput of the desired textbox
      Private Sub SetTextboxNumericOnly(sender As Object,
                                        e As TextCompositionEventArgs)
      
          Dim regex As New System.Text.RegularExpressions.Regex("[^0-9]+")
          e.Handled = regex.IsMatch(e.Text)
      
      End Sub
      

      请记住,您仍然需要检查文本框是否包含值,以防他们删除文本框的内容。此例程确保它始终是数字,因此不再需要检查。

      【讨论】:

        【解决方案7】:

        稍微切题,根据问题checking for numeric value entered in text box in visual basic的最高答案,还有方法.TryParse被认为是比IsNumeric更好的解决方案:

        第一个原因是,使用 TryParse 您还可以获得转换结果,而使用 IsNumeric 您必须在检查后进行转换。

        第二个原因是你可以给 IsNumeric 任何你想要的对象(例如也是一个按钮)并且它接受它。你永远不会在编译时发现这种错误。相反,使用 TryParse,您只能将字符串作为其第一个参数传递。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-11-25
          • 2021-08-18
          • 1970-01-01
          • 1970-01-01
          • 2013-03-05
          • 2013-07-25
          • 2023-03-27
          相关资源
          最近更新 更多