简体   繁体   English

在 VB.NET 中,如何让文本框只允许一个小数点,< 3NUMBERS 之前,只有 1NUMBER 之后?

[英]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.此外,用户无法在我希望的情况下在小数点前设置 2 个数字。

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 Private Sub TextBox11_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 处理 TextBox11.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.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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