2015-12-22 64 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「。」被輸入。