[英]Limiting the number of characters you can enter into a Windows Forms textbox
假设我有一个Windows窗体文本框,并希望减少通过用户输入允许的最大字符数,我该怎么做?
Set the MaxLength property. 设置MaxLength属性。 No code required, you can set it in the designer.
无需代码,您可以在设计器中进行设置。
您可以使用MaxLength属性设置文本框中允许的最大字符数。
Set MaxLength does not work with isNumber=true, you still can input 00012 regardless of limit = 4 设置MaxLength不适用于isNumber = true,无论limit = 4,您仍然可以输入00012
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.