c# - Keypress event is not working properly -


i'm having problems keypress events.

when enter 10 digits in textbox1 takes 10 digits want. if press backspace shows message "you can't enter more ten digits". first problem.

the second problem when clear textbox1 pressing backspace , enter digits again takes 9 digits.

please tell me what's going wrong code:

private void textbox1_keypress(object sender, keypresseventargs e) {   if (textbox1.text.trim().length > 9)   {       messagebox.show("you can't enter more ten digits...");       textbox1.maxlength = 9;    } } 

you can check if keychar backspace:

if (e.keychar != '\b' && textbox1.text.trim().length > 9) {     e.handled = true;     messagebox.show("you can't enter more ten digits..."); } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -