C# 富文本框 - 粗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17284573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rich Text Box - Bold
提问by Tom O
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!
知道什么可能导致这种情况吗?提前致谢!
采纳答案by LarsTech
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");
回答by Arclight
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.
因为随着文本框内容的增长,处理选择实体最终可能会变得很麻烦。
回答by Jonesopolis
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.
如果附加 @"\rtf1\ansi",则可以使用 \b 和 \b0 在字符串中声明粗体。而 \line 创建一个新行。你也可以做下划线等。我发现像这样构建字符串比应用属性更容易。
回答by Sujay
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 :)
希望这可以帮助 :)
回答by Bogdan
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
这是粗体

