c# - Method to highlighting Text in TextBox is inconsistent -
c# - Method to highlighting Text in TextBox is inconsistent -
currently using this method highlight text within textbox, works sometimes.
this code has verify if there contained space in entered text. if there space in text user should warned, , text within textbox has highlighted:
if (textbox.text.contains(" ")) { messagebox.show("sorry, value entered must not contain spaces.", "please come in valid value", messageboxbutton.ok, messageboximage.error); //highlights wrong text textbox.selectionstart = 0; textbox.selectionlength = textbox.text.length; }
why isn't method working me time , can prepare it?
it might problem when selecting length of textbox has no focus in current moment.
can seek add together check focus?
if (textbox.text.contains(" ")) { messagebox.show("sorry, value entered must not contain spaces.", "please come in valid value", messageboxbutton.ok, messageboximage.error); if(!textbox.focused) { textbox.focus(); } //highlights wrong text textbox.selectionstart = 0; textbox.selectionlength = textbox.text.length; }
also instead of current solution can utilize textbox.selectall():
if (textbox.text.contains(" ")) { textbox.selectall(); messagebox.show("sorry, value entered must not contain spaces.", "please come in valid value", messageboxbutton.ok, messageboximage.error); }
c# wpf textbox
Comments
Post a Comment