Textbox. maxlength can limit the length of the input content. But how can we limit the length of an ANSI string to be more precise?
In ANSI encoding, Chinese occupies 2 bytes and English 1 byte. textbox. maxlength cannot be limited to ANSI strings mixed with Chinese and English.
Consider the following method to limit the input length:
1. maxlength
2. Handling in keypress events
3. Process in textchanged
Dim oldtxt = ""
Private sub txtsingletxt_textchanged (sender as system. Object, e as system. eventargs) handles txtsingletxt. textchanged
Dim c = SMS. Global. maxlen-SMS. Global. txtlen (txtsingletxt. Text)
Lbcount3.text = C
If (C <0) then
Txtsingletxt. Text = oldtxt
Else
Oldtxt = txtsingletxt. Text
End if
End sub
Private sub tbparentsmsg_keypress (sender as system. Object, e as system. Windows. Forms. keypresseventargs) handles tbparentsmsg. keypress
Dim c = SMS. Global. maxlen-SMS. Global. txtlen (tbparentsmsg. Text)
Lbcount3.text = C
If (C <= 0 and (vbback <> E. keychar) then
E. Handled = true
End if
End sub