2014-04-03 53 views
0

我嘗試更改RichTextBox中某些單詞的顏色,最後我發現了一個代碼被剪掉了很多東西,但現在我遇到了問題。代碼僅在我的RichTextBox中着色第一個「NULL」。在RichTextBox中着色某些單詞

你能幫我找到解決辦法嗎?

感謝您的幫助!

private void searchWord() 
    { 
     String search = "NULL"; 
     TextPointer text = RTBAuftrag.Document.ContentStart; 
     while (true) 
     { 
      TextPointer next = text.GetNextContextPosition(LogicalDirection.Forward); 
      if (next == null) 
      { 
       break; 
      } 

      TextRange txt = new TextRange(text, next); 

      int indx = txt.Text.IndexOf(search); 
      if (indx > 0) 
      { 
       TextPointer sta = text.GetPositionAtOffset(indx); 
       TextPointer end = text.GetPositionAtOffset(indx + search.Length); 
       TextRange textR = new TextRange(sta, end); 
       textR.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Red)); 
      } 
      text = next; 
     } 
    } 

有人知道一個可能的方法來做到這一點嗎?

回答

0

即時回答我自己的問題! 最後,我找到了一個很好的解決方案,解決了我的問題。

I`ve發現了一個偉大的強大用戶控件的代碼PROJEKT http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor

我到finall解決方法是:

添加此我.XML

xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit" 

<avalonEdit:TextEditor Visibility="Hidden" x:Name="RTBAuftragNEU" Margin="32,413,243,279" KeyDown="RTBKunde_KeyDown" Panel.ZIndex="1" VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled" > </avalonEdit:TextEditor> 

然後把這個給我上課時我需要它。

 using (Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ConsultingControls.highlight.xshd")) 
     { 
      using (XmlTextReader reader = new XmlTextReader(s)) 
      { 
       RTBAuftragNEU.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance); 
      } 
     } 

不要忘記爲Syntax創建XSHD文件,您將在codeproject站點上找到完整的教程。

問候, fridolin