【问题标题】:How to check if text box max length has been exceeded?如何检查是否超过了文本框最大长度?
【发布时间】:2014-10-26 00:56:54
【问题描述】:

我的问题:

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

如果我放弃 .Maxlength 函数,而是使用 .Substring 来限制输入,我的 >8 函数就完成了,但是行为与 .Substring 不同(最后而不是前 8 个输入被保留,我失去了警报声)。

在不影响前 8 个输入的情况下,能够检查何时超过 .Maxlength 会更干净。

重现:

  1. 在 Visual Studio 中,在设计模式下,将文本框和工具提示拖到新表单上。
  2. 按原样使用以下内容:

代码:

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

【问题讨论】:

  • MaxLength 应该是设计时属性,因此您不必担心用户超过最大值,因为他们无法做到。
  • 使用ErrorProvider 可能会节省一些代码,而不是创建各种工具提示
  • @JoeEnos 用户永远无法超过最大值,但如果用户尝试超过最大值,我被要求显示气球提示。这是基于用户可能不知道他们为什么受到限制。关于您的设计时建议,在代码中设置 .MaxLength 是否有缺点?
  • 你的逻辑有缺陷。考虑用户输入非数字字符的情况。 TextBox1.Text.Length > 8 比较永远不会执行。
  • 您可能已经通过 LarsTech 的回答找到了最干净的解决方案 - 还有其他方法,也许可以处理 KeyPressKeyDown,但我认为这将是简单得多。关于 MaxLength,该属性是持久的,因此每次在事件处理程序中设置它是多余的 - 一旦设置它,它现在就像你在设计时完成它一样 - 这显然不是你想要做的为了得到你的预期行为。

标签: vb.net winforms visual-studio substring maxlength


【解决方案1】:

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

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

【讨论】:

  • 谢谢,LarsTech (+1)。这是让.Substring 表现得更像.Maxlength 的完美解决方案,但就我的整体问题而言,我仍然需要一种解决方法来满足> 8 条件以显示工具提示。
  • @ClarusDignus 它以我的形式显示。确保注释掉 MaxLength 行。
  • 道歉。我把不正确的行注释掉了。再次感谢。
【解决方案2】:

如果无效最好抑制:

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

编辑

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

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

解决一切问题:

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

【讨论】:

    【解决方案3】:

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

    TextBox1.SelectionStart = 8
    

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      相关资源
      最近更新 更多