Rich Text Box - Bold

Here's some code I used one time :

var sb = new StringBuilder();
        sb.Append(@"{\rtf1\ansi");
        sb.Append(@"\b Name: \b0 ");
        sb.Append((txtFirstName.Text);
        sb.Append(@" \line ");
        sb.Append(@"\b DOB: \b0 ");
        sb.Append(txtDOBMonth.Text);
        sb.Append(@" \line ");
        sb.Append(@"\b ID Number: \b0 ");
        sb.Append(txtIdNumber.Text);
        sb.Append(@" \line \line ");
        sb.Append(@"}");

richTextBox.Rtf = sb.ToString();

if you append @"\rtf1\ansi" you can use \b and \b0 to declare bold within the string. And \line creates a new line. You can also do underline, etc. I found it easier to build the String like this than applying properties.


This line is a problem:

textBox.Text += roomChatMessage.from + ": " + roomChatMessage.text + "\r\n";

You are replacing the formatting and the text with this new version of a string, and is probably picking up the bold font from the last update.

Try using AppendText instead:

textBox.AppendText(roomChatMessage.from + ": " + roomChatMessage.text + "\r\n");

 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 :)


In Visual Studio you can use this short code:

richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";

It will be:

This is in bold

Tags:

C#

Richtextbox