string - How to write cods or find text in vb.net -
i have 2 textbox controls
- one searching
- one text self
if click search button want search word in text box highlighted.
assuming building desktop application.
if want highlight first word in text content matches search word, can use string.indexof find word. can highlight using returned index:
- for winforms set selectionstart & selectionlength(see selecting text programmatically)
- for wpf can set selectionstart & selectionlength
for example in wpf add click event handler search button:
private sub searchbutton_click(sender object, e routedeventargs) ' find index of first match dim index = contentbox.text.indexof(searchbox.text) ' check match if index = -1 return ' set focus contentbox.focus() ' select first matching word contentbox.selectionstart = index contentbox.selectionlength = searchbox.text.length end sub
if want highlight all words in text content match search word, need call string.indexof multiple times using overload takes start index search. show multiple words highlighted need use richtextbox available in both wpf , winforms.
Comments
Post a Comment