c# - How can I highlight whatever substring I want in a custom drawn string? -
i have been working on own autocomplete combobox , textbox. @ first searched matches contained substring whatever in .text property of textbox or combobox. pretty intuitive algorithm user, little borderless listbox-on-a-form popup showed possible matches didn't need highlight substring in possible matches below.
eventually though started using autocomplete controls search strings this:
bob -- johnson -- 21 coconut grove -- age 43 -- customer id : 21253
i wanted make if user typed "bob coconut" or "bob coco 42" in box record appear in drop down, because contains "bob" , "coconut" in first case , because "bob" , "coco" in in second case though 42 not.
i want user figure out algorithm matches , sorts suggestions coloring different substrings appear in suggestions. here code fills suggestion popup listbox 1 item currently.
private void list_drawitem(object sender, drawitemeventargs e) { color bcolor = e.backcolor; if (e.state == drawitemstate.selected) { e.graphics.fillrectangle(new solidbrush(this.popupselectionbackcolor), e.bounds); e.graphics.drawstring(" " + this.list.items[e.index].tostring(), e.font, new solidbrush(this.popupselectionforecolor), e.bounds, stringformat.generictypographic); } else { //e.drawbackground(); e.graphics.fillrectangle(new solidbrush(this.backcolor), e.bounds); e.graphics.drawstring(" " + this.list.items[e.index].tostring(), e.font, new solidbrush(e.forecolor), e.bounds, stringformat.generictypographic); } }
what need figure out how find coordinates of substrings in match string, , either redraw them separate color or run kind of graphical algorithm on pixels background color in rectangle containing text changed.
i'm not familiar gdi+. suggestions?
i think function you're looking measurestring(). if break string before , after section want highlight, call measurestring on each substring you'll start , end points you're looking for.
Comments
Post a Comment