The input length of textbox is limited. The length of the input is two Chinese characters, one English byte.

Source: Internet
Author: User

 

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.