VB.NET:如何验证文本框不允许小数值?

时间:2012-10-11 02:56:56

标签: vb.net winforms decimal

我想知道如何验证文本框不允许任何小数值?

3 个答案:

答案 0 :(得分:1)

这个解决方案来自这个链接(How to allow user to enter only numbers in a textbox in 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

答案 1 :(得分:1)

您可以使用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

答案 2 :(得分:1)

如果可以,请使用MaskedTextBox

由于处理KeyPress会导致删除/退格/复制/粘贴/...

问题