Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color and font for some part of text in WPF C#

Is there a way to change color and font for some part of text which I want to put on TextBox or RichTextBox. I am using C# WPF.

For example

 richTextBox.AppendText("Text1 " + word + " Text2 "); 

Variable word for example to be other color and font from Text1 and Text2. Is it possible and how to do this?

like image 487
Ivan Tanasijevic Avatar asked Mar 26 '11 11:03

Ivan Tanasijevic


People also ask

What tag would you use to change the color of a few words or a section of text?

<FONT COLOR= > To change some of the text in the HTML document to another color use the FONT COLOR Tag. To change the color of the font to red add the following attribute to the code to the <FONT COLOR=" "> tag. #ff0000 is the color code for red.

How do you recolor text?

Go to Format > Font > Font. + D to open the Font dialog box. Select the arrow next to Font color, and then choose a color. Select Default and then select Yes to apply the change to all new documents based on the template.

Which command can be used to modify the color of the text which appears on screen?

Select the text that you want to change. On the Home tab, in the Font group, choose the arrow next to Font Color, and then select a color. You can also use the formatting options on the Mini toolbar to quickly format text.


2 Answers

If you just want to do some quick coloring, the simplest solution may be to use the end of the RTB content as a Range and apply formatting to it. For example:

TextRange rangeOfText1 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfText1.Text = "Text1 "; rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);  TextRange rangeOfWord = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfWord.Text = "word "; rangeOfWord.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red); rangeOfWord.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);  TextRange rangeOfText2 = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd); rangeOfText2.Text = "Text2 "; rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold); 

If you are looking for a more advanced solution, I suggest reading this Microsoft Doc about Flow Document, as it gives you a great flexibility in formatting your text.

like image 96
Gimno Avatar answered Sep 19 '22 10:09

Gimno


You can try this out.

public TestWindow() {     InitializeComponent();      this.paragraph = new Paragraph();     rich1.Document = new FlowDocument(paragraph);      var from = "user1";     var text = "chat message goes here";     paragraph.Inlines.Add(new Bold(new Run(from + ": "))     {         Foreground = Brushes.Red     });     paragraph.Inlines.Add(text);     paragraph.Inlines.Add(new LineBreak());     this.DataContext = this; } private Paragraph paragraph; 

Use the Document property of the RichTextBox.

like image 42
Learner Avatar answered Sep 18 '22 10:09

Learner