vb.net - VB 2010 - Changing the Color of a word in a Textbox (every copy of the word) -
hello , in advance,
i have textbox in text being generated. while it's being generated want color word "successfully" green , word "failed" red.
i'm using :
formtxtbox.find()("successfully") formtxtbox.selectioncolor = color.yellowgreen formtxtbox.selectionfont = new font(formtxtbox.font.fontfamily, formtxtbox.font.size, fontstyle.bold) formtxtbox.deselectall() formtxtbox.find("failed") formtxtbox.selectioncolor = color.red formtxtbox.selectionfont = new font(formtxtbox.font.fontfamily, formtxtbox.font.size, fontstyle.bold) formtxtbox.deselectall()
it's working problem have colours first "successfully" or "failed" string, whereas textbox has many copies of word in it. how can make color every copy of these words ?
yep, guil said, .find finds first occurrance.
https://msdn.microsoft.com/en-us/library/hfcsf75k(v=vs.110).aspx
i did find article , modified slightly:
https://support.microsoft.com/en-us/kb/176643
it recursively searches richtextbox, selects searched text, , changes text specified. you'd need add additional parameter font if you're changing well.
private sub button2_click(sender object, e eventargs) handles button2.click findit(me.richtextbox1, "failed", color.red) end sub private function findit(byref box richtextbox, byval search string, byval color color, optional start int32 = 0) int32 dim retval int32 'instr returns long dim source string 'variable used in instr try source = box.text 'put text search variable retval = source.indexof(search, start) 'do first search, 'starting @ beginning 'of text if retval <> -1 'there @ least 1 more occurrence of 'the string 'the richtextbox doesn't support multiple active selections, 'this section marks occurrences of search string 'making them bold , red box .selectionstart = retval .selectionlength = search.length .selectioncolor = color .deselectall() 'this line removes selection highlight end start = retval + search.length 'move starting point past 'first occurrence 'findit calls new arguments 'this makes recursive findit = 1 + findit(box, search, color, start) end if catch ex exception debug.writeline(ex.message) end try return retval end function
Comments
Post a Comment