Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use multi color in richtextbox [duplicate]

I using C# windows forms and I have richtextbox and I want to color some text in red, some in green and some in black.

How to do so? Image attached.

enter image description here

like image 956
Billie Avatar asked Nov 04 '12 17:11

Billie


People also ask

How to change Font color in RichTextBox c#?

Richtextbox font colorForeColor = Color. FromArgb(255, 108, 105, 105);

What is a rich text box?

A RichTextBox control is an advanced text box that provides text editing and advanced formatting features including loading rich text format (RTF) files.

What is the file format exclusively used in RichTextBox control?

The Windows Forms RichTextBox control can display a plain-text, Unicode plain-text, or Rich-Text-Format (RTF) file. To do so, call the LoadFile method. You can also use the LoadFile method to load data from a stream.

What is rich text box in visual basic?

The RichTextBox allows formatting the text. It also provides the facility to save the text in the file and restore it from the file. Drag and drop RichTextBox control from the toolbox on the window Form. ' SaveFile Method Saves the contents of the RichTextBox to a file.


1 Answers

System.Windows.Forms.RichTextBox has got a property of type Color of the name SelectionColor which gets or sets the text color of the current selection or insertion point. You can use this property to mark specific fields in your RichTextBox with the colors you specify.

Example

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Select(0, 8); //Select text within 0 and 8
_RichTextBox.SelectionColor = Color.Red; //Set the selected text color to Red
_RichTextBox.Select(8, 16); //Select text within 8 and 16
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green
_RichTextBox.Select(0,0); //Select text within 0 and 0

Notice that: You may avoid calculations by using RichTextBox.Find(string str) which can be added through Object Browser if you would like to highlight the text within the Lines in RichTextBox giving it's value

Example

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Find("Account 12345, deposit 100$, balance 200$"); //Find the text provided
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green

Thanks,
I hope you find this helpful :)

like image 158
Picrofo Software Avatar answered Oct 06 '22 01:10

Picrofo Software