vb.net 限制您可以在 Windows 窗体文本框中输入的字符数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9048776/
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
Limiting the number of characters you can enter into a Windows Forms textbox
提问by HunderingThooves
Assuming that I have a Windows Forms textbox and want to reduce the maximum amount of characters that can be allowed in via user entry, how would I do that?
假设我有一个 Windows 窗体文本框,并希望减少通过用户输入允许的最大字符数,我该怎么做?
回答by Hans Passant
Set the MaxLength property. No code required, you can set it in the designer.
设置 MaxLength 属性。无需代码,您可以在设计器中进行设置。
回答by Darin Dimitrov
回答by Tiny Gipxy
Set MaxLength does not work with isNumber=true, you still can input 00012 regardless of limit = 4
设置 MaxLength 不适用于 isNumber=true,您仍然可以输入 00012 而不管 limit = 4
My solution:
我的解决方案:
Protected Overridable Sub szSeqNmbr_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles szAuthID.KeyPress
Dim KeyAscii As Short = Convert.ToInt32(eventArgs.KeyChar)
If (szAuthID.Text.Length >= szAuthID.MaxLength) Then
'szAuthID.Text = szAuthID.Text.Substring(0, szAuthID.MaxLength)
If (KeyAscii >= 48 And KeyAscii <= 57) Then
eventArgs.Handled = True
Return
End If
End If
End Sub