By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.
"Mr.Baha" <ba****@sbcglobal.net> wrote in message news:11**********************@c13g2000cwb.googlegr oups.com...
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.

Nov 16 '05 #2

P: n/a
Thanks Kai, this works perfectly for me.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.