2016-06-01 6 views
1

アプリケーションにRichTextBoxがあり、テキストファイル、正確には次のようなログファイルが読み込まれます。 http://pastebin.com/3ETeYSUH これが続きます。RichTextBox内の特定の色の行

私の質問は、「Sikertelencsatlakozá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(); 

答えて

0

は(私は手動ではなく、ファイルからテキストを追加しました)このコードを試してみてください:テキストUsing GetLineStartPosition to get the end of a line in WPF RichTextBoxし、最終的に着色:Change color and font for some part of text in WPF C#

を、 WPF Richtextbox Application.Find Text spanning Multiple runs行を取得:

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

私は、テキストを見つけることに詳細については、以下のスタックオーバーフローのリンクを利用しました

関連する問題