简体   繁体   中英

TextRange and RichTextBox in C# WPF

I have been trying to write a program to search for a word in a richTextBox. I have made the most part, but it looks like I have missed something. I want to color the found words so I've written the following:

private void button1_Click(object sender, RoutedEventArgs e)
    {
        richTextBox1.SelectAll();
        string words = richTextBox1.Selection.Text; // to get the the whole text

        int length = words.Length;                  // length of text

        string search = textBox1.Text;              // the word being searched for
        int search_length = search.Length;


        int index =0;                               // to go through the text
        int endIndex = 0;                           // the end of the got word

         // pointer to the begining and the ending of the word which will be colored.
        TextPointer start_pointer, end_pointer;   

        if(length>0 &&search_length>0)              // text exists
        while(index<length-search_length)  
        {
            index = words.IndexOf(search, index);
            if (index < 0) // not found
                break;
             endIndex = index+search.Length-1;       // last char in the word
             start_pointer = richTextBox1.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward).GetPositionAtOffset(index, LogicalDirection.Forward);
             end_pointer = richTextBox1.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward).GetPositionAtOffset(endIndex + 1, LogicalDirection.Forward);

            TextRange got = new TextRange(start_pointer, end_pointer);

 //just for debugging
            MessageBox.Show("start =" + index + " end =" + endIndex + " " + got.Text);

            got.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
            index =endIndex+1;
        }

the first word is colored. But the next words aren't (ie if the the text was "go to school, and I'll go to the market", with the word "go" to be search for, and I pressed on the search button the result would be coloring the first "go", but the second one wouldn't be colored).

I estimate that this occurs because the textRange is not working properly, or something is going wrong in TextPointer. furthermore, index and endIndex are correct - I have tested them.

I appreciate your help.

Try this code:

 TextRange rangeText = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);
 rangeText.Text = "Text1 ";
 rangeText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
 rangeText.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

 TextRange rangeWord = new TextRange(richTextBox.Document.ContentEnd,        richTextBox.Document.ContentEnd);
 rangeWord.Text = "word ";
 rangeWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
 rangeWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);

 TextRange rangeTextOne = new TextRange(richTextBox.Document.ContentEnd,     richTextBox.Document.ContentEnd);
 rangeTextOne.Text = "Text2 ";
 rangeTextOne.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
 rangeTextOne.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM