2015-12-22 68 views
1

如何使txtamount.text只接受十进制值,它应该只接受一个.不超过一个。TextBox.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 
+0

使用'NumericUpDown'而不是'TextBox' –

回答

0

您可以使用

 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 
0

我建议使用NumericUpDown来代替。它有一个名为DecimalPlaces的属性,您可以在其中设置用户可以输入多少个小数点的限制。那么你不需要任何代码只是为了验证1“。”被输入。