简体   繁体   English

为RichTextBox中的某些单词着色

[英]Coloring certain words in a RichTextBox

i try to change the color of certains words in a RichTextBox, finally i found a code snipped wich help me alot, but now i have a problem. 我尝试更改RichTextBox中某些单词的颜色,最后我发现一段代码被截断了,对我有很多帮助,但是现在我遇到了问题。 The code only colours up the first "NULL" in my RichTextBox. 该代码仅使RichTextBox中的第一个“ NULL”变色。

Can you help me to find a solution ? 您能帮我找到解决方案吗?

Thanks for your helping! 感谢您的帮助!

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

Does somebody know a possible way to do this ? 有人知道这样做的可能方法吗?

im going to answer my question by myself! 我要自己回答我的问题! Finally i found a great solution for my problem. 终于,我找到了解决我问题的好方法。

I`ve found a great powerfull UserControl on code-projekt http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor 我在代码程序http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor上找到了一个功能强大的UserControl

My way to the finall solution was: 我的最终解决方案是:

Adding this to my .xml 将此添加到我的.xml

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

And this 和这个

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

Then put this to my class where i need it. 然后将其放在我需要的班上。

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

Dont forget to create the XSHD file for the Syntax, you will find a complete tutorial on the codeproject site. 不要忘记为语法创建XSHD文件,您将在codeproject网站上找到完整的教程。

greetings, fridolin 问候,弗里多林

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM