How do I format text in a RichTextBox?
I am writing a chat program and I would like to present the name of the user in a different color. If the user sends or receives a message, I would like to display the user n开发者_StackOverflowame in blue withing a RichTextBox. How do I do this?
Just use the SelectionColor property to change the color of the text:
private void button1_Click(object sender, EventArgs e) {
richTextBox1.AppendText("blahblah\n");
Color prev = richTextBox1.SelectionColor;
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.AppendText("nobugz\n");
richTextBox1.SelectionColor = prev;
richTextBox1.AppendText("blahblah\n");
}
You can also use SelectionBackColor to change the background color.
Coloring text inside a rich textbox is a matter of providing the input as RTF. Have a look at this article on how to do so http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx
精彩评论