比较来自世界各地的卖家的域名和 IT 服务价格

如何检查是否超过了文本字段的最大长度?

我的问题:

我限制了文本字段 8 符号并显示超过时的弹出暗示 />8/, 而且没有实现 /=8/. 使用功能
.Maxlength

不允许用户超过 8 字符,所以我的功能 >8 从未执行过。

如果我拒绝这个功能
.Maxlength

而不是我使用
.Substring

限制输入,我的功能 >8 表演,但行为不同
.Substring

/保留后者,不是第一个 8 输入和我丢失了声音警报/.

能够何时可以检查它会更清洁
.Maxlength

超过不影响第一个 8 输入。

重现:

在 Visual Studio 在构造函数模式下,将文本字段和弹出尖端拖动到新表单。

使用以下内容:

代码

:


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
已邀请:

喜特乐

赞同来自:

更好地压制

钥匙

, 如果它无效:


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

有次要功能 "bug" 在
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

窦买办

赞同来自:

更换文本时,它会重置托架,因此将其移回到最后的位置:


TextBox1.Text = TextBox1.Text.Substring/0, 8/
TextBox1.Select/TextBox1.TextLength, 0/

詹大官人

赞同来自:

在调用子字符串后添加它


TextBox1.SelectionStart = 8

要回复问题请先登录注册