18

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!

6 Answers 6

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

0
20

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.

2
  • 1
    The other thing to make note of is that you assign this to .Rtf, not to .Text. Thanks for your help!
    – Chad
    Apr 21, 2014 at 18:45
  • Found a useful short tutorial on more rtf features: pindari.com/rtf1.html Oct 13, 2021 at 16:43
12

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");
0
6

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

2

I feel it may be easier to use the RichTextBox.Rtf property when performing this kind of action, as mentioned here:

MSDN: Code: Formatting Characters in Bold in a RichTextBox Control (Visual C#)

Since as the contents of your text box grows, handling selection entities may end up becoming cumbersome.

0

I know this is an old post, but I wanted to add this here as the MSDN Documentaion doesn't explain this very well. The property SelectionFont doesn't exist anymore at least in .Net 5

 RichTextBox.Selection.ApplyPropertyValue(FontWeightProperty, FontWeights.Bold);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.