C# – Rich Text Box – Bold

crichtextbox

I know there are loads of "how to bolden text" questions on here, but none of the answers are helping, I think it may be that the Rich Text Box is being created at runtime.

I'm making a chat client, so I have a rich text box split up in lines and the messages are as follows:
{Name}: {Message} \r\n

I want to bolden the name, I have tried many code examples, but this is the closest I've got to it working:

int length = textBox.Text.Length;
textBox.Text += roomChatMessage.from + ": " + roomChatMessage.text + "\r\n";
textBox.Select(length, roomChatMessage.from.Length);
textBox.SelectionFont = new Font(textBox.Font, FontStyle.Bold);

The first message, it works perfectly fine, the name is in bold. But when I add a second message, everything turns bold even though the second time round I'm selecting the start index (Which is this example is 37) but everything just turns bold, all the past messages too!

Any idea to what may cause this?
Thanks in advance!

Best Answer

 ObjRichTextBox.SelectionFont = new Font(ObjRichTextBox.Font, FontStyle.Bold);

 ObjRichTextBox.AppendText("BOLD TEXT APPEARS HERE");

 ObjRichTextBox.SelectionFont = new Font(ObjRichTextBox.Font, FontStyle.Regular);

 ObjRichTextBox.AppendText("REGULAR TEXT APPEARS HERE");

Hope this helps :)

Related Topic