0

I would like specific content of a richTextBox after it was loaded mark colored (the `background 'of certain words change). When I click on a button to e.g. the word car to be marked. Now I found this code that I have to rebuild for my purposes.

public bool DoSearch(RichTextBox richTextBox, string searchText, bool searchNext)
       {
            TextRange searchRange;

            // Get the range to search
            if (searchNext)
                searchRange = new TextRange(richTextBox.Selection.Start.GetPositionAtOffset(1),
                                            richTextBox.Document.ContentEnd);
            else
                searchRange = new TextRange(richTextBox.Document.ContentStart, 
                                            richTextBox.Document.ContentEnd);

            // Do the search
            TextRange foundRange = FindTextInRange(searchRange, searchText);
            if (foundRange == null)
                return false;

            // Select the found range
            richTextBox.Selection.Select(foundRange.Start, foundRange.End);
            return true;
        }

        public TextRange FindTextInRange(TextRange searchRange, string searchText)
        {
            // Search the text with IndexOf
            int offset = searchRange.Text.IndexOf(searchText);
            if (offset < 0)
                return null;  // Not found

            // Try to select the text as a contiguous range
            for (TextPointer start = searchRange.Start.GetPositionAtOffset(offset); 
                             start != searchRange.End; 
                             start = start.GetPositionAtOffset(1))
            {
                TextRange result = new TextRange(start, start.GetPositionAtOffset(searchText.Length));
                if (result.Text == searchText)
                    return result;

            }
            return null;
        }

can someone help me?

Karl_Schuhmann
  • 1,272
  • 5
  • 17
  • 37
  • I want to change the background from a word in a `richtextbox`. With this Code i can search a word and now i have to insert it in a flowdocument, change the background and take it back to the richtextbox at the same position befor. But i dont know how i can do this. – Karl_Schuhmann Feb 12 '13 at 10:47
  • Where is the FlowDocument coming from? You certainly don't need that. – Daniel Hilgarth Feb 12 '13 at 10:51
  • I was think that i need a `FlowDocument`. On start of the program i load a .txt-file with the `FlowDocument` in the `Richtextbox` – Karl_Schuhmann Feb 12 '13 at 10:57
  • 1
    This question helped me... The fool people doesn't know something about WPF. Some people like them are spoiling stackoverflow... – pcbabu Jan 31 '14 at 13:33

1 Answers1

1

Please try this link, contains a function HighlightPhrase which can change The Color of Specific Word in Rich textbox. Following this function try to investigate how to change background.

How to Change The Color of Specific Word in Rich textbox using C#

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71