سؤال

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. The code only colours up the first "NULL" in my RichTextBox.

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

My way to the finall solution was:

Adding this to my .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.

greetings, fridolin

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top