wpf - Retrieve the end position of the caret in the TextBox to put the Caret in the next TextBox -
how find end position of caret in wpf textbox can`t move right anymore caret?
if need find caretindex @ following question.
however, if looking jump next textbox under conditions check out following sample. here use textbox property maxlength , keyup event jump next textbox when 1 completed.
here xaml:
<grid> <grid.rowdefinitions> <rowdefinition/> <rowdefinition/> </grid.rowdefinitions> <stackpanel grid.row="0"> <textbox text="" maxlength="3" keyup="textbox_keyup" > </textbox> <textbox text="" maxlength="3" keyup="textbox_keyup"> </textbox> <textbox text="" maxlength="4" keyup="textbox_keyup"> </textbox> </stackpanel> </grid>
here keyup event code-behind:
private void textbox_keyup(object sender, keyeventargs e) { textbox tb = sender textbox; if (( tb != null ) && (tb.text.length >= tb.maxlength)) { int nextindex = 0; var parent = visualtreehelper.getparent(tb); int items = visualtreehelper.getchildrencount(parent); for( int index = 0; index < items; ++index ) { textbox child = visualtreehelper.getchild(parent, index) textbox; if ((child != null) && ( child == tb )) { nextindex = index + 1; if (nextindex >= items) nextindex = 0; break; } } textbox nextcontrol = visualtreehelper.getchild(parent, nextindex) textbox; if (nextcontrol != null) { nextcontrol.focus(); } } }
edit:
after reading following answer modified textbox_keyup follows:
private void textbox_keyup(object sender, keyeventargs e) { action<focusnavigationdirection> movefocus = focusdirection => { e.handled = true; var request = new traversalrequest(focusdirection); var focusedelement = keyboard.focusedelement uielement; if (focusedelement != null) focusedelement.movefocus(request); }; textbox tb = sender textbox; if ((tb != null) && (tb.text.length >= tb.maxlength)) { movefocus(focusnavigationdirection.next); } } }
Comments
Post a Comment