简体   繁体   English

富文本框 - 粗体

[英]Rich Text Box - Bold

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我正在制作一个聊天客户端,所以我有一个分行的富文本框,消息如下:{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!但是当我添加第二条消息时,即使第二轮我选择了开始索引(这个例子是 37),一切都变成了粗体,但一切都变成了粗体,所有过去的消息也是如此!

Any idea to what may cause this?知道什么可能导致这种情况吗? Thanks in advance!提前致谢!

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

richTextBox.Rtf = sb.ToString();

if you append @"\\rtf1\\ansi" you can use \\b and \\b0 to declare bold within the string.如果附加 @"\\rtf1\\ansi",则可以使用 \\b 和 \\b0 在字符串中声明粗体。 And \\line creates a new line.而 \\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:尝试使用 AppendText 代替:

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

In Visual Studio you can use this short code:在 Visual Studio 中,您可以使用以下简短代码:

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:我觉得在执行此类操作时使用 RichTextBox.Rtf 属性可能更容易,如下所述:

MSDN: Code: Formatting Characters in Bold in a RichTextBox Control (Visual C#) MSDN:代码:在 RichTextBox 控件中设置粗体字符格式 (Visual C#)

Since as the contents of your text box grows, handling selection entities may end up becoming cumbersome.随着文本框内容的增长,处理选择实体最终可能会变得很麻烦。

I know this is an old post, but I wanted to add this here as the MSDN Documentaion doesn't explain this very well.我知道这是一篇旧帖子,但我想在这里添加它,因为MSDN 文档并没有很好地解释这一点。 The property SelectionFont doesn't exist anymore at least in .Net 5至少在.Net 5 ,属性SelectionFont不再存在

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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