【问题标题】:Color specific lines in RichTextBox为 RichTextBox 中的特定行着色
【发布时间】:2016-06-02 12:16:10
【问题描述】:

我的应用程序中有一个 RichTextBox,它读取一个文本文件,一个确切的日志文件,如下所示: http://pastebin.com/3ETeYSUH 就这样继续下去。

我的问题是,如何将整条线涂成红色,上面写着“Sikertelen csatlakozás”? (是的,匈牙利语,意思是:“无法连接”)

我读取文件的代码:

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();

【问题讨论】:

    标签: c# wpf richtextbox


    【解决方案1】:

    试试这段代码(我只是手动添加文本而不是从文件中添加):

        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);
                    }
                }
            }
        }
    

    我使用以下 Stack Overflow 链接了解查找文本的详细信息:WPF Richtextbox Application.Find Text spanning Multiple runs,获取线路:Using GetLineStartPosition to get the end of a line in WPF RichTextBox,最后为文本着色:Change color and font for some part of text in WPF C#

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2022-01-12
      相关资源
      最近更新 更多