Rich Text Box - Bold

寵の児 提交于 2019-12-04 00:14:53

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

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(@"}");

rtxtRequest.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.

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!