1

I have a RichTextBox in my application, that reads a text file, a log file exactly, that looks like this: http://pastebin.com/3ETeYSUH And that goes on.

My question is, how can I color the full line to RED that says "Sikertelen csatlakozás"? (Yes, hungarian language, it means: "Can't connect")

My code that reads the file:

string fileName = "servermanagerlog.txt";
            TextRange range;
            FileStream fStream;
            if (File.Exists(fileName))
            {
                range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                fStream = new FileStream(fileName, FileMode.OpenOrCreate);
                range.Load(fStream, DataFormats.Text);
                fStream.Close();
            }
            richTextBox.ScrollToEnd();

1 Answers1

0

Try this code (I just added text in manually rather than from file):

    void richTextBox_Loaded(object sender, RoutedEventArgs e)
    {
        TextRange range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        range.Text = "Here is one line with some text" + Environment.NewLine;
        range.Text += "Here is another line Sikertelen csatlakozás with some text" + Environment.NewLine;
        range.Text += "Here is another line with some text" + Environment.NewLine;
        range.Text += "Here is another line Sikertelen csatlakozás with some text" + Environment.NewLine;
        range.Text += "Here is one line with some text" + Environment.NewLine;
        range.Text += "Here is another line Sikertelen csatlakozás with some text" + Environment.NewLine;
        range.Text += "Here is another line with some text" + Environment.NewLine;
        range.Text += "Here is another line Sikertelen csatlakozás with some text" + Environment.NewLine;

        String strSearch = "Sikertelen csatlakozás";
        int start = range.Text.IndexOf(strSearch);

        TextPointer tp = null;

        if (start > -1)
        {
            tp = range.Start.GetPositionAtOffset(start);
        }

        while (tp != null)
        {
            TextPointer tpLine = tp.GetLineStartPosition(0);

            TextPointer tpLineEnd = tp.GetLineStartPosition(1);
            TextPointer lineEnd = (tpLineEnd !=null ? tpLineEnd : tp.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward);

            TextRange redText = new TextRange(tpLine, lineEnd);
            redText.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
            tp = tpLineEnd;

            if (tp != null)
            {
                start = range.Text.IndexOf(strSearch, start + 1);

                if (start > -1)
                {
                    tp = range.Start.GetPositionAtOffset(start);
                }
            }
        }
    }

I utilized the following Stack Overflow links for specifics on finding text: WPF Richtextbox Application.Find Text spanning Multiple runs, getting the line: Using GetLineStartPosition to get the end of a line in WPF RichTextBox and finally coloring the text: Change color and font for some part of text in WPF C#

Community
  • 1
  • 1
David Glass
  • 61
  • 1
  • 5