sczdavos
sczdavos

Reputation: 2063

C# RichTextBox colored text

I've a RichTextBox and I want color text in it. Is there any tag option? I'd like something like this [color:red]nick[/color] some message. Because I need to save it as text and I want on reload have also colored text.

Can I do something like this without writing own method?

Upvotes: 4

Views: 12340

Answers (4)

Siva
Siva

Reputation: 130

rtfMain.SaveFile(dlgSave.FileName);

From Reference Save text from rich text box with C#

Upvotes: -1

Pedery
Pedery

Reputation: 3636

If you need examples of how things are encoded in RTF, you can create the document manually in Word or Wordpad, and save it as RTF. This will give you a hint about how to encode your formatting. Furthermore, if you're for instance creating help-documents, you can include them as an embedded resource and load them directly into the RichTextBox, with all the formatting included.

Upvotes: 0

codeteq
codeteq

Reputation: 1532

As the name says RichTextBox contains RichText

to change the Rtf text with 'rtf specific-tags' you can set/use the

RichTextBox.RtfProperty

also take a look at RichTextBox.SelectionColor to color text patterns in code

but when you don't want to use rtf, you said

need to save as text.

you could write your own 'markup' there is no built in expect rtf/html?

but rtf is text - at all

Example to use RichTextBix.SelectionColor to Color the text

richTextBox1.Text = "Hello";
richTextBox1.Select(0,2);
richTextBox1.SelectionColor = Color.Red;

colors the start of "Hello" red and now you can access the 'taggeg' text in the RTFProperty of the RichTextBox

Upvotes: 3

JleruOHeP
JleruOHeP

Reputation: 10376

You can set color for text in RichTextBox with SelectionColor

And if you want to save your rtf as plain text, then you will have to look at rtf format. Example:

{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;} This line is the default color\line \cf2 This line is red\line \cf1 This line is the default color }

EDIT:

From this example - first of all you have to declare color table \colortbl in fololowing format:

{\colortbl; color1; color2; ... ; colorN;} 

And then in the text you will have to enclose text with {\cfN YOUR_TEXT} where N is a number of color from table; you can not specify the boundaries of the block {}, then everything after \ cfN will be one color.

Upvotes: 7

Related Questions