-
Change RichTextBox Color
I want to change specific text color in a RichTextBox. I want to insert something with a specific color. The solutions I find by googling require, if I understand correctly, that you read again all the text and then paint it accordingly. Can't you just insert something, color it and stay colored?
The methods I use is Select() to select a text and SelectionColor to change it. It seems simple but it doesn't really work for me.
Like, will this color blue str inserted to output rich text box?
Code:
output.Text += str;
output.Select(output.Text.Length - 1 - str.Length, str.Length);
output.SelectionColor = Color.Blue;
-
If output.Text is initially empty, and str is of length 3, you'll start selection at index 3 - 1- 3 = -1. That's not what you want, is it?
-
Probably not. Eh, it was just a quick example, i should emmit the -1.
What I get is color the text. But then whatever I insert to the richbox it also goes blue. I am close but can't seem to get it working.
-
Haven't tested anything, but it sounds like a feature, not a bug, that appended text will use the font/style/color of the last character of the existing text.
As for a quick fix why not always set a color, black if no other is specified?
-
That was my main idea. But it didn't work properly. I ll try it more and see...
-
OK.
The RichTextBox has two properties.
Text and Rtf.
The text is the string it displays. The Rtf is the full string, which is the Text and some parameters about various things.
The thing is that when you do this:
then every color that you have set will be lost. So I cannot do += to update the text.
It actually just takes the Text and wraps it with a header like "{options\blah\blah USEFUL TEXT \options\blah\blah}". I you select something and change the color you will get the right thing, but when you want again to insert text it will take the updated text and wrap around a header.
There has to be a standard way to add text with a certain color. Obviously doing:
Code:
r.Text += "somestring";
doesn't work as I want, but what else can I do? I could of course create my own method for doing so, but there has to be one already
-
Code:
private void AppendColoredText(System.Windows.Forms.RichTextBox RichTextBox, string Text, System.Drawing.Color Color)
{
var StartIndex = RichTextBox.TextLength;
RichTextBox.AppendText(Text);
var EndIndex = RichTextBox.TextLength;
RichTextBox.Select(StartIndex, EndIndex - StartIndex);
RichTextBox.SelectionColor = Color;
}
-
OK. I found a workaround. But there should be a better way to do this.
Code:
Util.outputRtf = "A basic RTF format text";
...
public void Out(string str, int color)
{
string s;
switch (color)
{
case 1:
s = "\\cf2 ";
break;
case 2:
s = "\\cf3 ";
break;
case 3:
s = "\\cf4 ";
break;
case 4:
s = "\\cf5 ";
break;
default:
s = "\\cf0 ";
break;
}
s += str;
s = s.Replace("\r\n", "\\par ");
int ind = Util.outputRtf.IndexOf("\\cf1 #");
Util.outputRtf = Util.outputRtf.Insert(ind, s);
output.Rtf = Util.outputRtf;
output.SelectionStart = output.Text.Length;
output.ScrollToCaret();
}
The logic is that I get a basic RTF format from a RichTextBox. I copy the ".Rtf" property value and save it to Util.outputRtf (a string inside my static Util class).
I insert a line where the ".Text" starts. The line is "\\cf1 #". Basically i use # to indicates the end of the Text abd \\cf1 makes it white, so it doesn't show.
The \\cfX changes colors from a list I have inserted in the Util.outputRtf string.
This way I can insert a string in the RichTextBox and set the color from a set of colors I have.
I could make something more complicated and general for all colors, but I don't really need it and it would be less efficient anyway.
There are some functions out there like http://www.codeproject.com/KB/edit/csexrichtextbox.aspx
-
Lol, I missed the AppendText method (slaps himself)
Edit: Thanx Magos