简体   繁体   中英

In VB.NET how do I get a textbox to only allow one decimal point, < 3NUMBERS before it, and only 1NUMBER after it?

As the question suggests, I need a textbox to only allow one decimal point in it, less than three numbers before it, and only one number after it.

I've compiled this code so far.

Private Sub TextBox14_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox14.KeyPress
    Dim keyChar = e.KeyChar

    If Char.IsControl(keyChar) Then
        'Allow all control characters.
    ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
        Dim text = Me.TextBox14.Text
        Dim selectionStart = Me.TextBox14.SelectionStart
        Dim selectionLength = Me.TextBox14.SelectionLength

        text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)

        If Integer.TryParse(text, New Integer) AndAlso text.Length > 3 Then
            'Reject an integer that is longer than 16 digits.
            e.Handled = True
        ElseIf Double.TryParse(text, New Double) AndAlso text.IndexOf("."c) < text.Length - 3 Then
            'Reject a real number with two many decimal places.
            e.Handled = True
        End If
    Else
        'Reject all other characters.
        e.Handled = True
    End If
End Sub

The biggest issue I'm getting is that the user can put in multiple decimal points and then basically all the rules I created go away. Additionally, the user is not able to set 2 numbers before the decimal point when I want them to.

Solved it using my head.

Private Sub TextBox11_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox11.KeyPress 'What is allowed to be typed in sale price textbox Dim keyChar = e.KeyChar

    If Char.IsControl(keyChar) Then
        'Allow all control characters.
    ElseIf Char.IsDigit(keyChar) OrElse keyChar = "."c Then
        Dim text = Me.TextBox11.Text
        Dim selectionStart = Me.TextBox11.SelectionStart
        Dim selectionLength = Me.TextBox11.SelectionLength

        text = text.Substring(0, selectionStart) & keyChar & text.Substring(selectionStart + selectionLength)
        If TextBox11.Text.Contains("."c) Then
            'Forbids a user from entering in two decimal places
            If keyChar = "."c Then
                e.Handled = True
            ElseIf text.Length - text.IndexOf("."c) > 3 Then
                e.Handled = True
            End If
        Else 'no decimal point currently in textbox
            If text.Length > 5 And keyChar = ("."c) Then 'Allows only a "." to be written 
                e.Handled = False
            ElseIf text.Length > 5 Then ' Numbers before decimal point above 99,999
                e.Handled = True
            End If
        End If
    Else
        'Reject all other characters for this textbox.
        e.Handled = True
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM