丰富的文本框 - 粗体 [英] Rich Text Box - Bold

查看:202
本文介绍了丰富的文本框 - 粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道也有如何博尔登文字的问题在这里,但没有一个答案是帮助,我想这可能是因为正在运行时创建的格式文本框的负载。

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.

我在做一个聊天客户端,所以我有一个丰富的文本框线条分割,并且是信息如下:
{}名称:{}留言\r\ ñ

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



第一条消息,这工作完全正常,名称为黑体。但是,当我添加第二个消息,一切都会变得即使绕第二圈时,我选择起始索引(这是本例子是37),但一切都只是变成黑体加粗,过去的一切消息呢!

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!

推荐答案

这行是有问题的:

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.

尝试使用AppendText通过代替:

Try using AppendText instead:

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

这篇关于丰富的文本框 - 粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆