Use the selectionstart attribute in C # To specify the cursor position in the input box

Source: Internet
Author: User

A small bug needs to be modified in today's work. The requirement is to automatically remove 0 if the number starts with 0 during text box input.

If you enter 012, 12 is displayed.

It is easy to judge in the text changed event of textbox. If the text is startwith 0, trimstart ('0 ')

However, in actual debugging, it is found that 0 is removed, but the cursor is kept at the beginning. Then, the user enters 012 three numbers in sequence, and the interface is displayed as 21.

After viewing the data, we found that the control property selectionstart in C # can specify the cursor position, so you only need to add a sentence after processing trimstart ('0 ')

Textbox. selectionstart = textbox. Text. length to meet the requirements

In addition, the selectionstart attribute can be get and set, which can be used to complete various small demands for cursor positioning or obtaining the cursor position.

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.