c# - Highlighting keywords in a richtextbox in WPF -


i'm making program needs through paragraph of text , find how many times keyword/keywords appear. has highlight each of these key words in text.

i have managed make interface , can track how many times word appears stuck how highlight keywords appear. post code below, appreciated on how search , highlight text inside richtextbox. since in wpf obvious richtextbox.find() not avaliable use.

class textanalyser {     public int findnumberofoccurances(list<string> keywords, string email)     {         int occurances = 0;         foreach (string keyword in keywords)         {             occurances += email.toupper().split(new string[] { keyword.toupper() }, stringsplitoptions.none).count() - 1;          }         return occurances;     }      public void turntextred(list<string> keywords, string email, richtextbox textbox)     {         foreach(string keyword in keywords)         {         }     }      public list<string> converttexttolist(string text)     {         char[] splitchars = {};         string[] arraytext = text.split( splitchars, stringsplitoptions.removeemptyentries);         return arraytext.tolist<string>();     }      public string getstringfromtextbox(richtextbox textbox)     {         var textrange = new textrange(             textbox.document.contentstart,             textbox.document.contentend         );         return textrange.text;     } } 

and here main window

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();     }      private void analysebutton_click(object sender, routedeventargs e)     {         var texttool = new textanalyser();         var keywords = texttool.converttexttolist(texttool.getstringfromtextbox(wordtextbox).trim());         var email = texttool.getstringfromtextbox(emailtextbox).trim();         int usesofword = texttool.findnumberofoccurances(keywords, email);         occurances.text = usesofword.tostring();     } } 

here method used of word in richtextbox's document.

 public static ienumerable<textrange> getallwordranges(flowdocument document)      {          string pattern = @"[^\w\d](\w|[-']{1,2}(?=\w))*";          textpointer pointer = document.contentstart;          while (pointer != null)          {              if (pointer.getpointercontext(logicaldirection.forward) == textpointercontext.text)              {                  string textrun = pointer.gettextinrun(logicaldirection.forward);                  matchcollection matches = regex.matches(textrun, pattern);                  foreach (match match in matches)                  {                      int startindex = match.index;                      int length = match.length;                      textpointer start = pointer.getpositionatoffset(startindex);                      textpointer end = start.getpositionatoffset(length);                      yield return new textrange(start, end);                  }              }               pointer = pointer.getnextcontextposition(logicaldirection.forward);          }      } 

you can change pattern used split words.

at last, easy highlight words.

  ienumerable<textrange> wordranges = getallwordranges(richtextbox.document);         foreach (textrange wordrange in wordranges)         {             if (wordrange.text == "keyword")             {                 wordrange.applypropertyvalue(textelement.foregroundproperty, brushes.red);             }         } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -