简体   繁体   English

VB.NET:如何验证文本框不允许十进制值?

[英]VB.NET: How to validate a textbox to not allow decimal values?

我想知道如何验证文本框以不允许任何十进制值?

this solution i got from this link ( How to allow user to enter only numbers in a textbox in vb.net? ) 我从此链接得到的解决方案( 如何允许用户在vb.net的文本框中仅输入数字?

   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
              Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
            e.Handled = True
    End If
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
            e.Handled = False
    End If
End Sub

You can use the KeyPress event and use the IsNumeric Function to trap the numeric keys. 您可以使用KeyPress事件并使用IsNumeric函数来捕获数字键。

Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If IsNumeric(e.KeyChar) Then
        e.Handled = True
    End If
End Sub

If you can, use a MaskedTextBox 如果可以,请使用MaskedTextBox

Since handling the KeyPress can cause problem with delete/backspace/copy/paste/... 由于处理KeyPress可能导致删除/退格/复制/粘贴/ ...问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM