【问题标题】:Validation on Keypress using VB.NET使用 VB.NET 对 Keypress 进行验证
【发布时间】:2014-07-15 10:03:34
【问题描述】:

此代码在 Keypress 事件中

当我尝试输入 -1、-2 或 1.1 -1.3 时没有问题,但是当我尝试在文本框中输入 1/2 和 1/4 时,我只在这个“1/”上存货。 我认为我的代码有问题,但根本没有错误消息,我无法弄清楚。 . 任何形式的帮助和建议都非常感谢。 .

    Dim tb As TextBox = CType(sender, TextBox)  
    Dim chr As Char = e.KeyChar  

    If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then  
        e.Handled = Not IsNumeric(tb.Text & e.KeyChar)  
    ElseIf IsNumeric(e.KeyChar) And Not e.KeyChar = "/" Then  
        e.Handled = Not IsNumeric(tb.Text & e.KeyChar)  
    ElseIf e.KeyChar = "." Then  
        If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then  
            e.Handled = True  
        End If  
    ElseIf e.KeyChar = "/" Then  
        If tb.SelectionStart <> 1 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "/" Then  
            e.Handled = True  
        End If  
    ElseIf e.KeyChar = "-" Then  
        If tb.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "-" Then  
            e.Handled = True  
        End If  
    ElseIf Not Char.IsControl(e.KeyChar) Then  
        e.Handled = True  
    End If  

【问题讨论】:

  • “仅存货” 1/ 是什么意思?
  • @rontornambe 我认为 OP 的意思是,“他只停留在 1/”,只是我的猜测......
  • 我想像这样输入:“1/2”,但是当我这样做时,这个“1/”上的库存我不能再添加任何数字了。
  • "1/2" 不是数字。顺便说一句,这个词是“卡住”。强烈建议使用调试器单步调试此类代码。

标签: vb.net vb.net-2010


【解决方案1】:

您的第一个条件本身的问题,您检查 isumeric 由于“/”符号而返回 false。

  Dim tb As TextBox = CType(sender, TextBox)
        Dim chr As Char = e.KeyChar

        If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
            If tb.Text.Contains("/") = False Then
                e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
            End If

        ElseIf IsNumeric(e.KeyChar) And Not e.KeyChar = "/" Then
            e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
        ElseIf e.KeyChar = "." Then
            If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
                e.Handled = True
            End If
        ElseIf e.KeyChar = "/" Then
            If tb.SelectionStart <> 1 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "/" Then
                e.Handled = True
            End If
        ElseIf e.KeyChar = "-" Then
            If tb.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "-" Then
                e.Handled = True
            End If
        ElseIf Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If

【讨论】:

  • 或者您也可以评论该声明。
  • 我尝试了你的代码。 .收到此错误:从字符串“1/2”到类型“Double”的转换无效。
  • 我的这部分代码出现错误 If txta1.Text = 1 Then numa1 = "x" ElseIf txta1.Text = -1 Then numa1 = "-x" Else numa1 = txta1.Text + "x" 结束如果
【解决方案2】:

我认为你可以通过测试整个值是否是数字而不是测试每个字符来避免这个错误和很多复杂性。例如:

If IsNumeric(tb.Text) Then
   e.Handled = true
End If

你不能这样做有什么原因吗?

【讨论】:

  • 这行不通。 OP 正在尝试将数字 放入 TextBox。
  • 那又怎样? IsNumeric 处理数字、小数点、分数等。我在这里缺少什么?
  • 您的代码说,如果 TextBox 中有一个数字,请停止所有键盘操作。因此,一旦您输入“1”,您就不能在 TextBox 中添加另一个数字。另外,IsNumeric 不能处理分数,所以 IsNumeric("1/2") 返回 false。
  • 你是对的。太晚了,无法回答这些问题。
猜你喜欢
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2013-03-11
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
相关资源
最近更新 更多