基于VB.NET的按键验证

基于VB.NET的按键验证,vb.net,vb.net-2010,Vb.net,Vb.net 2010,此代码在按键事件下 当我尝试输入-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 = "-"

此代码在按键事件下

当我尝试输入-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  
Dim tb As TextBox=CType(发送方,TextBox)
Dim chr As Char=e.KeyChar
如果IsNumeric(e.KeyChar)而不是e.KeyChar=“-”则
e、 Handled=notisnumeric(tb.Text&e.KeyChar)
ElseIf为数字(e.KeyChar)而不是e.KeyChar=“/”则
e、 Handled=notisnumeric(tb.Text&e.KeyChar)
ElseIf e.KeyChar=“”然后
如果不是(tb.SelectedText=“”或IsNumeric(tb.Text&e.KeyChar)),则
e、 已处理=真
如果结束
ElseIf e.KeyChar=“/”然后
如果tb.SelectionStart 1或Microsoft.VisualBasic.Left(tb.Text,1)=“/”则
e、 已处理=真
如果结束
ElseIf e.KeyChar=“-”然后
如果tb.SelectionStart 0或Microsoft.VisualBasic.Left(tb.Text,1)=“-”,则
e、 已处理=真
如果结束
如果不是Char.IsControl(例如KeyChar),则
e、 已处理=真
如果结束

您的问题与第一个条件本身有关,您在其中检查isnumeric,由于“/”符号返回false

Dim tb As TextBox=CType(发送方,TextBox)
Dim chr As Char=e.KeyChar
如果IsNumeric(e.KeyChar)而不是e.KeyChar=“-”则
如果tb.Text.Contains(“/”)为False,则
e、 Handled=notisnumeric(tb.Text&e.KeyChar)
如果结束
ElseIf为数字(e.KeyChar)而不是e.KeyChar=“/”则
e、 Handled=notisnumeric(tb.Text&e.KeyChar)
ElseIf e.KeyChar=“”然后
如果不是(tb.SelectedText=“”或IsNumeric(tb.Text&e.KeyChar)),则
e、 已处理=真
如果结束
ElseIf e.KeyChar=“/”然后
如果tb.SelectionStart 1或Microsoft.VisualBasic.Left(tb.Text,1)=“/”则
e、 已处理=真
如果结束
ElseIf e.KeyChar=“-”然后
如果tb.SelectionStart 0或Microsoft.VisualBasic.Left(tb.Text,1)=“-”,则
e、 已处理=真
如果结束
如果不是Char.IsControl(例如KeyChar),则
e、 已处理=真
如果结束

我认为,通过测试整个值是否为数字,而不是测试每个字符,可以避免此错误和大量复杂性。例如:

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

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

什么是“只储存在这个”1/“上?@rontornanbe我想OP的意思是,“他只停留在1/”上,我猜……我想这样输入:“1/2”,但当我这样做时,它储存在这个“1/”上,我不能再添加任何数字了。“1/2”不是数字。单词是“卡住”“,顺便说一句。强烈建议使用调试器单步执行此类型的代码。或者您也可以对该语句进行注释。我尝试了您的代码,但。获取此错误:从字符串“1/2”到类型“Double”的转换无效。如果txta1.Text=1,然后numa1=“x”ElseIf txta1.Text=-1,然后numa1=“-x”Else numa1=txta1.Text+“x”End,如果这不起作用,则获取这部分代码的错误。OP试图把数字输入文本框。那又怎样?IsNumeric处理数字、小数点、分数等。我这里缺少什么?您的代码说,如果文本框中有数字,请停止所有键盘操作。因此,一旦输入“1”,就不能在文本框中添加其他数字。另外,IsNumeric不能处理分数,所以IsNumeric(“1/2”)返回false。你说得对。晚上回答这些问题太晚了。
If IsNumeric(tb.Text) Then
   e.Handled = true
End If