繁体   English   中英

TextBox.Text仅接受十进制输入

[英]TextBox.Text accept decimal inputs only

如何使txtamount.text仅接受十进制值,应仅接受一个十进制值. 不超过一个。

我的追随者是我的尝试,但可以接受更多.

Private Sub txtamount_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtamount.KeyPress
        If Asc(e.KeyChar) <> 8 Then
            If (Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57) And Asc(e.KeyChar) <> 46 Then
                e.Handled = True
            End If
        End If
    End Sub

您可以使用

        Dim asccode As Integer = Asc(e.KeyChar)
        If asccode <> 8 Then
            If asccode = 46 And txtPackageAmount.Text.Contains(".") Then
                e.Handled = True
            End If
            If (asccode < 48 Or asccode > 57) And asccode <> 46 Then
                e.Handled = True
            End If
        End If

我建议改用NumericUpDown 该属性具有一个称为DecimalPlaces的属性,您可以在其中设置用户可以输入的小数位数的限制。 这样,您将不需要任何代码即可仅验证1个“”。 输入。

暂无
暂无

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

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