我正在尝试验证放置在 TextBox
内的字符数,但遇到了一些麻烦。我使用的代码如下:
If Not ((TextBox5.Text.Length) <= 1) Or ((TextBox5.Text.Length) >= 10) Then
MsgBox("Invalid date entry. Use the the following format: DD-MM-YYYY.")
TextBox5.Focus()
TextBox5.SelectAll()
Else
'do whatever
End If
我想要的是 TextBox5
的长度在 1 和 10 之间(含),如果不重新选择 TextBox
使其为另一个用户输入做好准备。
该代码对于小于 1 个字符的输入响应良好,但无法识别任何大于 10 个字符的输入。我看不出我做错了什么?
最佳答案
首先,不要调用Focus
。文档明确指出,不要调用 Focus
。如果您想要聚焦某个控件,请调用其 Select
方法。
不过你也不需要打电话。您应该处理 Validating
事件,如果控件验证失败,则将 e.Cancel
设置为 True
并且控件不会在以下位置失去焦点第一名。
If myTextBox.TextLength < 1 OrElse myTextBox.TextLength > 10 Then
'Validation failed.
myTextBox.SelectAll()
e.Cancel = True
End If
关于vb.net - 检查 TextBox.Text.Length 是否在 1 到 10 之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37923422/