2011-06-19 71 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 
相关问题