question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Append Text with Color?

See original GitHub issue

Hi, new to this control. I’d like to append text to the box with a set color. With the WinForms RichTextBox I would do something like this:

        private void AppendTextToLogBoxWithColor(string text, Color color)
        {
            richTextBoxLog.SelectionStart = richTextBoxLog.TextLength;
            richTextBoxLog.SelectionLength = 0;

            richTextBoxLog.SelectionColor = color;
            richTextBoxLog.AppendText(text);
            richTextBoxLog.SelectionColor = richTextBoxLog.ForeColor;
        }

I was reading the documentation and saw that I can define custom syntax highlighting rules but is there a simple way to accomplish the same thing with AvalonEdit? Simply appending text with a specific color?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
derekantricancommented, Nov 11, 2020

Ok, it took quite a bit of digging through the code base, messing around, etc but I found a basic enough example for my situation:

(Note that I am using a ported version of AvalonEdit: AvaloniaEdit])

RichTextModel richTextModel = new RichTextModel();

public MainWindow()
{
    InitializeComponent();

    TextEditor avaloniaEdit = this.FindControl<TextEditor>("log");
    avaloniaEdit.TextArea.TextView.LineTransformers.Add(new RichTextColorizer(richTextModel));
}

private void AppendTextWithColor(string text, Color color)
{
    TextEditor avaloniaEdit = this.FindControl<TextEditor>("log");
    avaloniaEdit.AppendText(text);
    richTextModel.ApplyHighlighting(avaloniaEdit.Text.Length - text.Length, text.Length, new HighlightingColor() { Foreground = new SimpleHighlightingBrush(color)});
}

Thanks for all your help!

1reaction
dgrunwaldcommented, Oct 7, 2020

AvalonEdit has some classes that can help with implementing something like a rich text editor: ICSharpCode.AvalonEdit.Highlighting.RichTextModel is a data structure that can store font style/weight/color; RichTextColorizer can apply these during rendering. This way the colors don’t need to be part of the TextDocument. Note that you’ll manually have to hook these classes up to the editor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Color different parts of a RichTextBox string
Here is an extension method that overloads the AppendText method with a color parameter: public static class RichTextBoxExtensions { public ...
Read more >
Append/combine text in two cells with font color retained
I am trying to Append text from one column to other column using formula ... How can we combine text in two cells...
Read more >
Append Text to RichTextBox with color - CSharp
Description. Append Text to RichTextBox with color. Demo Code. public class Main{ //for RichTextBox public static void AppendText(this System.Windows.Forms.
Read more >
Changing Colors
To add a color to the background, use an attribute of the <BODY> tag known as BGCOLOR. Most newer browsers understand colors specified...
Read more >
how can I append colored text to RichEditControl by code?
Hi, The AppendText method appends a plain text string to a document and returns the DocumentRange object, which you can format (e.g., change...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found