c# - How do you search for a string in a rich text box and highlight all found or highlight each line? -
i found next code http://www.dotnetcurry.com/showarticle.aspx?id=146 , implemented app, finds string 1 time , go on looking other instances of string have maintain pressing search button (bit tedious 100's of matches).
i'd find instances of search string , if possible highlight each line, failing highlight string item code instances not one.
on link above there perchance solution downwards farther on page it's in vb don't know how convert c#.
private void btnlistsearch_click(object sender, eventargs e) { int startindex = 0; if (txtsearch.text.length > 0) startindex = findmytext(txtsearch.text.trim(), start, rtb.text.length); // if string found in richtextbox, highlight if (startindex >= 0) { // set highlight color reddish rtb.selectioncolor = color.red; // find end index. end index = number of characters in textbox int endindex = txtsearch.text.length; // highlight search string rtb.focus(); rtb.select(startindex, endindex); // mark start position after position of // lastly search string start = startindex + endindex; } } public int findmytext(string txttosearch, int searchstart, int searchend) { // unselect searched string if (searchstart > 0 && searchend > 0 && indexofsearchtext >= 0) { rtb.undo(); } // set homecoming value -1 default. int retval = -1; // valid starting index should specified. // if indexofsearchtext = -1, end of search if (searchstart >= 0 && indexofsearchtext >=0) { // valid ending index if (searchend > searchstart || searchend == -1) { // find position of search string in richtextbox indexofsearchtext = rtb.find(txttosearch, searchstart, searchend, richtextboxfinds.none); // determine whether text found in richtextbox1. if (indexofsearchtext != -1) { // homecoming index specified search text. retval = indexofsearchtext; } } } homecoming retval; } private void txtsearch_textchanged(object sender, eventargs e) { // reset richtextbox when user changes search string start = 0; indexofsearchtext = 0; }
i tried search listbox find strings @ start of line not along line.
string searchstring = textbox2.text; listboxresults.selectionmode = selectionmode.multiextended; // set our intial index variable -1. int x = -1; // if search string empty exit. if (searchstring.length != 0) { // loop through , find each item matches search string. { // retrieve item based on previous index found. starts -1 searches start. x = listboxresults.findstring(searchstring, x); // if no item found matches exit. if (x != -1) { // since findstring loops infinitely, determine if found first item 1 time again , exit. if (listboxresults.selectedindices.count > 0) { if (x == listboxresults.selectedindices[0]) return; } // select item in listbox 1 time found. listboxresults.setselected(x, true); } } while (x != -1); }
your code has few problems.
you phone call search , selection routines 1 time per button click, 1 occurrence can found each time. if want highlight occurrences need create loop
you undo each alter in findmytext
method. makes no sense. may want clear selections before search new, not while searching
you set selection color before setting selection. nothing, @ best..
here version stores positions in list , supports 2 buttons go forwards , backward through list..:
list<int> found = null; private void cb_findall_click(object sender, eventargs e) { int cursorpos = rtb.selectionstart; clearhighlights(rtb); found = findall(rtb, txtsearch.text, 0); highlightall(rtb, color.red, found, txtsearch.text.length); rtb.select(cursorpos, 0); } private void cb_findnext_click(object sender, eventargs e) { int pos = -1; (int f = 0; f < found.count; f++) if (found[f] > rtb.selectionstart) { pos = found[f]; break; } if (pos >= 0) rtb.select(pos, txtsearch.text.length); rtb.scrolltocaret(); } private void cb_findprev_click(object sender, eventargs e) { int pos = -1; (int f = 0; f < found.count; f++) if (found[f] >= rtb.selectionstart) { if (f >= 1) pos = found[f - 1]; break; } if (pos >= 0) rtb.select(pos, txtsearch.text.length); rtb.scrolltocaret(); } public list<int> findall(richtextbox rtb, string txttosearch, int searchstart) { list<int> found = new list<int>(); if (txttosearch.length <= 0) homecoming found; int pos= rtb.find( txttosearch, searchstart, richtextboxfinds.none); while (pos >= 0) { found.add(pos); pos = rtb.find(txttosearch, pos + txttosearch.length, richtextboxfinds.none); } homecoming found; } public void highlightall(richtextbox rtb, color color, list<int> found, int length) { foreach (int p in found) { rtb.select(p, length); rtb.selectioncolor = color; } } void clearhighlights(richtextbox rtb) { int cursorpos = rtb.selectionstart; // store cursor rtb.select(0, rtb.textlength); // select rtb.selectioncolor = rtb.forecolor; // default text color rtb.select(cursorpos, 0); // reset cursor } private void txtsearch_textchanged(object sender, eventargs e) { found = new list<int>(); // clear list clearhighlights(rtb); // clear highlights } private void rtb_textchanged(object sender, eventargs e) { found = new list<int>(); clearhighlights(rtb); }
note how little each piece of code is!
c# listbox richtextbox
No comments:
Post a Comment