2011-06-19 80 views
2

在窗口文本框中,我只想允許只有2個小數點。我可以只設置數字文本框,但不知道如何限制2個小數點。只允許2個小數點

例如,743.56

我的代碼如下

Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress   
    'allow numeric 
    If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then 
     e.Handled = True 
    End If 

    ' only allow one decimal point 
    If e.KeyChar = "."c AndAlso TryCast(sender, TextBox).Text.IndexOf("."c) > -1 Then 
     e.Handled = True 
    End If 

    End Sub 

如何?

+0

按鍵響應不正確的事件。或者還不夠。例如,它不會正確處理粘貼。 –

+0

@Serge - appTranslator,用於粘貼,如何? – soclose

回答

4

嘗試使用NumericUpDown而不是TextBox。 A NumericUpDown可讓您specify the number of decimal places to go to,並自動將其限制爲數字。你也有兩個方便的小上下按鈕。

+0

我設置'小數位'2,但它沒有工作。 – soclose

+0

那個控件很醜。它也沒有像預期的那麼好。您可以輸入許多小數點分隔符,只有在丟失焦點時纔會清除它們。 –

0

現在,它的工作。請檢查下面

 '2 decimal points only 
    'If key-in is after decimal point 
    If txtPrice.SelectionStart > txtPrice.Text.IndexOf(Chr(46)) Then 
     'If text not select All 
     If txtPrice.SelectedText.Length = 0 Then 
      If (txtPrice.Text.Trim() <> "") Then 
       If (rexPrice.IsMatch(txtPrice.Text) = False) AndAlso e.KeyChar <> ControlChars.Back Then 
        e.Handled = True 
       End If 
      End If 
     End If 
    End If 
2

我的代碼,一個簡單的辦法是隻使用:

txtPrice.Text = String.Format("{0:n2}", numberVariableHere); 
0

試試這個代碼(這將允許帶小數點僅數):

Private Sub txtCostPrice_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCostPrice.KeyPress 
    If InStr(txtCostPrice.Text, ".") And e.KeyChar = "." Then e.Handled = True 
    If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then 
     If e.KeyChar <> "." Then e.Handled = True 
    End If 
End Sub 
+0

這沒有解決OP的問題誰想要的數字在小數點後至多有兩位數 –

+0

它並不是僅有的用戶輸入後兩位數。 – DareDevil

1

這裏是我測試過的代碼,我只允許用戶在小數點後輸入一位數字;您可以將值2更改爲您的選擇。

Private Sub tb_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tb.KeyPress 
    If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." Then 
     e.Handled = True 
    Else 
     If e.KeyChar = "." And tb.Text.IndexOf(".") <> -1 Then 
      e.Handled = True 
     ElseIf e.KeyChar = "." Then 
      e.Handled = False 
     ElseIf Char.IsDigit(e.KeyChar) Then 
      If tb.Text.IndexOf(".") <> -1 Then 
       If tb.Text.Length >= tb.Text.IndexOf(".") + 2 Then 'replace 2 for greater numbers after decimal point 
        e.Handled = True 
       End If 
      End If 
     End If 
    End If 
End Sub 

我已經測試此代碼以獲取最新2,3,4小數點

0

我認爲這個代碼的幫助你解決你的問題

dim n as integer 
n=0 
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 
     If e.KeyChar = Chr(46) Then 
      n = Len(TextBox1.Text) 
     End If 
     If Len(TextBox1.Text) >= n + 2 And n <> 0 Then 
      TextBox1.Enabled = False 
     End If 
    End Sub 
0
Private Sub txtVatRate_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtVatRate.KeyPress 
    OnlyAllowPostiveNumbers(sender, e, 1) 
End Sub 

Public Function OnlyAllowPostiveNumbers(sender As Object, e As System.Windows.Forms.KeyPressEventArgs, Optional ByRef iDeimalPlaces As Integer = 0) As System.Windows.Forms.KeyPressEventArgs 
    'Only allow numeric values with the exception of spaces ie '07969 860053' and backspace 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then 
     e.Handled = True 
    End If 
    'Backspace 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then 
     e.Handled = False 
    End If 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) = 46) And InStr(sender.text, ".") < 1 Then 
     e.Handled = False 
    End If 
    If (Microsoft.VisualBasic.Asc(e.KeyChar) > 48) And (Microsoft.VisualBasic.Asc(e.KeyChar) < 57) Then 
     If InStr(sender.text, ".") > 0 Then 
      Dim n As Integer = InStr(sender.text, ".") 
      If n <= (sender.text.length - iDeimalPlaces) Then 
       e.Handled = True 
      End If 
     End If 
    End If 
    Return e 
End Function 
相關問題