Beerlol
Beerlol

Reputation: 357

Method to search through a RichTextBox and highlight all instances of that specific word

I'm really clueless with where I should begin to start with this.

I have a WPF application which has a RichTextBox, inside this there is a load of text using a FlowDocument which changes depending upon the user's selection.

I need a method from which a user can type a word into a TextBox and every instance of this word if it is found will then be highlighted with in the adjacent RichTextBox. http://kentb.blogspot.com/2009/06/search-and-highlight-text-in-arbitrary.html This idea would be perfect but I am clueless as how to apply it to my application with a RichTextBox.

Thank you in advance!

Upvotes: 2

Views: 5494

Answers (2)

paparazzo
paparazzo

Reputation: 45106

I do it with a FlowDocument. This sample lists the colors with a background of that color. I use FlowDocumentReader to display the FlowDocument but I think a RichTextBox will also display a FlowDocument. It might seem a little complex but marking up the actual text is way less problematic than having to highlight a position like I had to back with Windows.Form RichTextBox. This is code I used to decide what color highlight looked the best.

docFlowDocument = new FlowDocument();           
System.Windows.Media.Brush defaultBrush = System.Windows.Media.Brushes.White;
docFlowDocument.Background = defaultBrush;
System.Windows.Media.Brush curBrush = defaultBrush;
Paragraph p = new Paragraph();
Run r = new Run();
r.Background = curBrush;
#region nullDocument
 if (String.IsNullOrEmpty(DocText))
 {
     r.Foreground = System.Windows.Media.Brushes.Red;
     r.Text = "No Text";
     p.Inlines.Add(r);
     docFlowDocument.Blocks.Add(p);


     List<string> colorNames = (from pc in typeof(Brushes).GetProperties()
                                    select pc.Name).ToList();
     //Debug.WriteLine(colorNames.Count.ToString());
     //Debug.WriteLine(colorNames[0]);

     Type brushesType = typeof(Brushes);
     System.Reflection.MemberInfo[] membersinfo = brushesType.GetMembers();
     System.Reflection.PropertyInfo[] properties = brushesType.GetProperties();

     for (int i = 0; i < properties.Length; i++)
     {
         r = new Run();
         r.Background = (Brush)properties[i].GetValue(null, null);
         r.Text = colorNames[i];
         p.Inlines.Add(r);
         p.Inlines.Add(new LineBreak());
     }
     docFlowDocument.Blocks.Add(p);
     docFlowDocumentFinishedLastRun = true;
     return docFlowDocument;
 }
#endregion // nullDocument

Upvotes: 0

dotToString
dotToString

Reputation: 230

Have you tried using RegularExpressions?

Something like:

private void searchButton_Click(object sender, EventArgs e)
{
    //Select all text and bring it back to default color values so you
    //can make a new search selection

    richTextBox1.SelectAll();
    richTextBox1.SelectionColor = System.Drawing.Colors.Black;

    //Deselect all text to ready selections

    richTextBox1.DeselectAll();

    //Create a MatchList variable and initialize it to all matches
    //within the RichTextBox. Add a using statement of 
    //System.Text.RegularExpressions 

    Color evenColor = Color.Red;
    Color oddColor = Color.Blue;

    MatchCollection matches = Regex.Matches(richTextBox1.Text,  searchTextBox.Text);

    //Apply color to all matching text
    int matchCount = 0;
    foreach (Match match in matches)
    {
        richTextBox1.Select(match.Index, match.Length);
        //richTextBox1.SelectionColor = System.Drawing.Color.Red;
        richTextBox1.SelectionColor = 
            matchCount++ % 2 == 0 ? evenColor : oddColor;
    }
}

As long as you don't need multiple colors in your box at the same time, this method works. With some extra logic you could incorporate that, too, I'm sure.

edit: doesn't work in WPF. Keeping post up for WinForms.

Upvotes: 3

Related Questions