vb.net 计算TextBox中的字符数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37860212/
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
Counting number of characters in TextBox
提问by ABANDOND ACOUNT
I have a TextBoxby which if the user enters more than 10 characters it displays a MsgBox. That part works :D
我有一个TextBox,如果用户输入超过 10 个字符,它会显示一个MsgBox. 那部分有效:D
The problem is the message also displays if the TextBoxis empty and the user types the first character. I think thats because Nullis seen as something greater than 10? but I'm not sure.
问题是如果TextBox为空并且用户键入第一个字符,消息也会显示。我认为那Null是因为被视为大于 10?但我不确定。
A) What is going on?
A) 发生了什么?
B) How can I fix this?
B) 我该如何解决这个问题?
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Length >= 10 Then
MsgBox("WARNING")
End If
End Sub
回答by Student
You can try this. By using trim, white space characters is ignored. For an example, if the user only entered 10 [Spacebar] keys it will trim it out.
你可以试试这个。通过使用trim,空白字符将被忽略。例如,如果用户只输入了 10 个 [Spacebar] 键,它会将其修剪掉。
Private Sub TextBox3_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox3.KeyPress
If TextBox3.Text.Trim().Length() >= 10 Then
MsgBox("WARNING")
End If
End Sub

