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.