IneedHelp
IneedHelp

Reputation: 1758

How to change highlight colour of a portion of a text in rich text box?

I am new to WPF's RichTextBox. I would like to know how to highlight text on a line with a specific colour.

Let's say I have a rich text box with a yellow background and assign to it a flow document.

richTextBox.Background = Brushes.LightYellow;
var mcFlowDoc = new FlowDocument();
var para = new Paragraph();
para.Inlines.Add(new Run("This is the first line.\n"));
para.Inlines.Add(new Run("This is the second line.\n"));
para.Inlines.Add(new Run("This is the third line."));
mcFlowDoc.Blocks.Add(para);
richTextBox.Document = mcFlowDoc;

What would I have to do next to change the third line's highlight colour to red? I am not talking about selection highlight colour, but normal text highligting (like in WordPad)

If there is a solution, I would like it in C# code, I want to stay away from XAML editing.

Upvotes: 1

Views: 1728

Answers (1)

paparazzo
paparazzo

Reputation: 45106

    Run run = new Run("Red is the third line.\n");
    // run.Foreground = Brushes.Red;
    run.Background = Brushes.Red;
    para.Inlines.Add(run);

Upvotes: 2

Related Questions