简体   繁体   English

如何在C#中加粗传递给TextBox的一部分字符串

[英]How to bold a part of string being passed into TextBox in c#

I have a string builder where value is appending at runtime via placeholder. 我有一个字符串生成器,其中值在运行时通过占位符附加。 Once string builder has appended, it is assigned to a text box (not rich txt) to show up in UI. 附加了字符串生成器后,会将其分配给文本框(非RTX文本)以显示在UI中。

I want part of the text to be bold. 我希望部分文字为粗体。

sb.AppendFormat("Added {0} by {1}:\n{2}", DateTime.ToString(), userName, note);
txt.Text = sb.ToString();

Expected output: 预期产量:

Added 9/01/2016 8:47:19 PM by Vinoth: Testing Purpose 由Vinoth于9/01/2016 8:47:19 PM添加:测试目的

How can I achieve this? 我该如何实现? Is there anyway of looping over words with the : symbol until I want it to be bold? 无论如何,直到我希望将其变为粗体时,才使用:符号循环其单词?

You will have to build up the text in sections. 您将必须在各节中构建文本。

Either as different TextBoxes/TextBlocks or as a single RichTextBox with separate Runs for the sections you want in a different style. 作为不同的TextBoxes / TextBlocks或具有不同样式的单个RichTextBox,它们具有单独的“运行”。

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

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