【问题标题】:VB.Net check punctuationVB.Net检查标点符号
【发布时间】:2021-03-31 10:11:27
【问题描述】:

我有一个按键事件,将输入限制为只有这样的数字:

Private Sub txtWeight_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWeight.KeyPress

    If Not Char.IsNumber(e.KeyChar) Then
        e.Handled = True
    End If

End Sub

但我也想添加输入句点的可能性,所以我尝试将代码更改为:

Private Sub txtWeight_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtWeight.KeyPress

    If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsPunctuation(e.KeyChar) Then
        e.Handled = True
    End If

End Sub

但正如您所知,该程序允许我输入每个标点符号,我想将其限制为仅“。”如何查看输入的标点符号?

【问题讨论】:

    标签: .net vb.net events keypress


    【解决方案1】:
    e.Handled = e.KeyChar <> "."c AndAlso Not Char.IsNumber(e.KeyChar)
    

    【讨论】:

    • 谢谢,没想到这么简单,我改成:"If Not Char.IsNumber(e.KeyChar) AndAlso Not e.KeyChar = "." AndAlso Not Char.IsControl(e .KeyChar) Then" 并且完美运行
    • @NomeCognome,当有 &lt;&gt; 运算符时,使用 Not= 是不好的形式,但是,如果你要这样做,那么这仍然是不好的形式,因为你应该重构到If Not (Char.IsNumber(e.KeyChar) OrElse e.KeyChar = "." OrElse Char.IsControl(e.KeyChar)) Then
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多