C# WinForm....changing font colors for multi line rich textbox

M

Mr.Baha

Hello,
I have a situation where I am appending text to a multi-line
(rich)textbox in a C# form. Now depending on which event does the
appendtext, i want to distinguish the lines in the textbox by having
them written in different font colors. So my multi-line textbox will
contain lines of various colored text.


I tried setting the textboxe's ForeColor property, but quickly realized
that the fore color will change every line within the textbox, instead
of the individual pen color. I'm now reading up on the Font
constructor, but I don't see any method to change the font color, just
a bunch of style related parameters is what i see.
Does anyone have any suggestions?
Thank you in advance.
 
K

Kai Brinkmann [MSFT]

You can use SelectedText to accomplish this. For example, if you had an RichTextBox names rtResults:

Font fBold = new Font("Tahoma", 8, FontStyle.Bold);
rtResults.SelectionFont = fBold;
rtResults.SelectionColor = Color.Red;
rtResults.SelectedText = "Text output";
This code fragment would sent the text "Text output" to the text box rtResults, using the specified font in red.

Keep in mind that these settings won't carry over to the next line you write. It sounds like you're writing one line at the time, though, so that shouldn't be a problem. You could create a method that takes the required information as parameters (text to write, font, color, etc.) and writes one line to the textbox.

I hope this helps.

--
Kai Brinkmann [MSFT]


Please do not send e-mail directly to this alias. This alias is for newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top