[英]TextBox.Text accept decimal inputs only
How to make txtamount.text
accept only decimal values,it should accept only one .
如何使
txtamount.text
仅接受十进制值,应仅接受一个十进制值.
not more than one. 不超过一个。
My following is my try but it accept more .
我的追随者是我的尝试,但可以接受更多
.
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
You can use 您可以使用
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
I recommend using a NumericUpDown instead. 我建议改用NumericUpDown 。 That has a property called DecimalPlaces where you can set the limit of how many decimals the user can enter.
该属性具有一个称为DecimalPlaces的属性,您可以在其中设置用户可以输入的小数位数的限制。 Then you would not need any code just to validate only 1 "."
这样,您将不需要任何代码即可仅验证1个“”。 is entered.
输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.