仅允许2个小数点

时间:2011-06-19 07:05:34

标签: vb.net winforms

在窗口文本框中,我只想允许 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

如何?

7 个答案:

答案 0 :(得分:4)

尝试使用NumericUpDown而不是TextBoxNumericUpDown允许您specify the number of decimal places to go to,并自动将其限制为数字。你还有两个方便的小上下按钮。

答案 1 :(得分:2)

更简单的解决方案是使用:

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

答案 2 :(得分: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小数点

答案 3 :(得分: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

答案 4 :(得分: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

答案 5 :(得分: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

答案 6 :(得分: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