c# - how to add dots automatically while writing in TextBox -


i have 1 textbox binding on datetime type. need dot after first 2 chars , second 2 chars, example: 12.12.1990. i'm using behavior in textchanged event, code:

void tb_textchanged(object sender, textchangedeventargs e) {     int = tb.selectionstart;     if (i == 2 || == 5)     {                         tb.text += ".";         tb.selectionstart = + 1;     } } 

that working, if want delete text backspace, can't delete dots, because event called again.

what better way solve it?

solved

it works if can, may fix algorithm.

        public string oldtext = "";         public string currtext = "";         private void textbox1_textchanged(object sender, textchangedeventargs e)         {             oldtext = currtext;             currtext = textbox1.text;             if (oldtext.length > currtext.length)             {                 oldtext = currtext;                 return;             }             if (textbox1.text.length == currtext.length)             {                 if (textbox1.selectionstart == 2 || textbox1.selectionstart == 5)                 {                     textbox1.text += ".";                     textbox1.selectionstart = textbox1.text.length;                 }             }          } 

i in keypress event, can filter kind of key (using keychar argument char.isletter() , similar functions).

also, add dot when next key pressed. if user has typed "12", don't add dot yet. when user presses 1 add second "12", add (before new character).


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -