vb.net 如何检查文本框的最大长度是否已超出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26534314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:24:04  来源:igfitidea点击:

How to check if text box max length has been exceeded?

vb.netwinformsvisual-studiosubstringmaxlength

提问by Clarus Dignus

My problem:

我的问题:

I'm limiting a text box to 8 characters and showing a tooltip when it's exceeded (>8) rather than reached (=8). Using the .Maxlengthfunction prevents the user from ever exceeding 8 characters so my >8 function is never fulfilled.

我将文本框限制为 8 个字符,并在超过 (>8) 而不是达到 (=8) 时显示工具提示。使用该.Maxlength功能可以防止用户超过 8 个字符,因此我的 >8 功能永远不会实现。

If I forgo the .Maxlengthfunction and instead use .Substringto limit the input, my >8 function is fulfilled however the behavior differs from .Substring(the last rather than first 8 inputs are kept and I lose the alert sound).

如果我放弃该.Maxlength功能而是使用.Substring限制输入,则我的 >8 功能已完成,但行为与.Substring(保留最后而不是前 8 个输入并且我失去警报声)不同。

It would a lot cleaner to be able to check for whenever .Maxlengthis exceeded without affecting the first 8 inputs.

能够在.Maxlength不影响前 8 个输入的情况下检查何时超出会更清晰。

To reproduce:

重现:

  1. In Visual Studio, in design mode, drag a text box and tooltip onto a fresh form.
  2. Use the following as is:
  1. 在 Visual Studio 中,在设计模式下,将文本框和工具提示拖到新表单上。
  2. 按原样使用以下内容:

Code:

代码:

Public Class Form1
    Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.MaxLength = 8
        If (Not IsNumeric(TextBox1.Text) And TextBox1.Text.Length > 0) Then
            If ToolTip1.GetToolTip(TextBox1) = "" Then
                ToolTip1.ToolTipTitle = "Input must be numeric!"
                ToolTip1.Active = True
                ToolTip1.IsBalloon = True
                ToolTip1.ToolTipIcon = ToolTipIcon.Warning
                ToolTip1.Show(vbNewLine, TextBox1, 45, -40)
            End If
        ElseIf TextBox1.Text.Length > 8 Then
            'TextBox1.Text = TextBox1.Text.Substring(0, 8)
            ToolTip1.IsBalloon = True
            ToolTip1.ToolTipTitle = "8 character maximum!"
            ToolTip1.Active = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Warning
            ToolTip1.Show(vbNewLine, TextBox1, 45, -40)
        Else
            ToolTip1.Active = False
            ToolTip1.Hide(TextBox1)
        End If
    End Sub
End Class

采纳答案by LarsTech

When you replace the text, it resets the caret, so move it back into place at the end:

当您替换文本时,它会重置插入符号,因此在最后将其移回原位:

TextBox1.Text = TextBox1.Text.Substring(0, 8)
TextBox1.Select(TextBox1.TextLength, 0)

回答by γηρ?σκω δ' αε? πολλ? διδασκ?με

It is better to supress the keyif it is invalid:

如果密钥无效,最好将其抑制:

Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim str As String

    str = TextBox1.Text
    str = str.Insert(TextBox1.SelectionStart, CStr(e.KeyChar))

    If e.KeyChar = ChrW(Keys.Back) Then
        HideToolTip()
    ElseIf str.Length > 8 Then
        ShowToolTip("8 character maximum!")

        e.Handled = True
    ElseIf Not IsNumeric(str) Then
        ShowToolTip("Input must be numeric!")

        e.Handled = True
    Else
        HideToolTip()
    End If

End Sub

Private Sub HideToolTip()
    If ToolTip1.GetToolTip(TextBox1) <> "" Then
        ToolTip1.Active = False
        ToolTip1.Hide(TextBox1)
    End If
End Sub

Private Sub ShowToolTip(ByVal str As String)
    'always check if tooltip is visible, to avoid inversion
    If ToolTip1.GetToolTip(TextBox1) = "" Then
        ToolTip1.ToolTipTitle = str
        ToolTip1.Active = True
        ToolTip1.IsBalloon = True
        ToolTip1.ToolTipIcon = ToolTipIcon.Warning
        ToolTip1.Show(vbNewLine, TextBox1, 45, -40, 1000)
    End If
End Sub

EDIT

编辑

There is a minor "bug" in IsNumeric()function as it allows numeric with spaces and multiple "."

IsNumeric()函数中有一个小“错误”,因为它允许带有空格和多个“.”的数字。

8..888 'is numeric
.9999  'is numeric

To solve everything:

解决一切:

Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim str As String = "0123456789."

    If e.KeyChar = ChrW(Keys.Back) Then
        HideToolTip()
    ElseIf TextBox1.Text.Length = 8 Then
        ShowToolTip("8 character maximum!")

        e.Handled = True
    ElseIf e.KeyChar = "." And (TextBox1.Text.Contains(".") Or TextBox1.SelectionStart = 0) Then 'supress a second "." or a first one
        ShowToolTip("Input must be numeric!")

        e.Handled = True
    ElseIf Not str.Contains(CStr(e.KeyChar)) Then
        ShowToolTip("Input must be numeric!")

        e.Handled = True
    Else
        HideToolTip()
    End If

End Sub

回答by UnhandledExcepSean

Add this after the substring call

在子字符串调用之后添加这个

TextBox1.SelectionStart = 8