2014-04-03 49 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