简体   繁体   中英

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 name 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM