c# - 如何在richtextbox中使用多色

标签 c# .net richtextbox

<分区>

我使用 C# windows 窗体并且我有 richtextbox,我想将一些文本着色为红色,一些文本为绿色,一些文本为黑色。

怎么做?附上图片。

enter image description here

最佳答案

System.Windows.Forms.RichTextBox 有一个名为 SelectionColorColor 类型的属性,它获取或设置文本颜色当前选择或插入点。您可以使用此属性以您指定的颜色标记 RichTextBox 中的特定字段。

示例

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Select(0, 8); //Select text within 0 and 8
_RichTextBox.SelectionColor = Color.Red; //Set the selected text color to Red
_RichTextBox.Select(8, 16); //Select text within 8 and 16
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green
_RichTextBox.Select(0,0); //Select text within 0 and 0

注意:您可以使用 RichTextBox.Find(string str) 避免计算,如果您愿意,可以通过 Object Browser 添加它喜欢在 RichTextBox 中突出显示 Lines 中的文本,赋予其值(value)

示例

RichTextBox _RichTextBox = new RichTextBox(); //Initialize a new RichTextBox of name _RichTextBox
_RichTextBox.Find("Account 12345, deposit 100$, balance 200$"); //Find the text provided
_RichTextBox.SelectionColor = Color.Green; //Set the selected text color to Green

谢谢,
希望这对您有所帮助:)

关于c# - 如何在richtextbox中使用多色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13220856/

相关文章:

c# - 在 Asp .Net MVC Composition Root 中配置 IoC 容器的最佳实践

c# - 你认为 "auto interface implementation"在 .NET/C# 中有用吗?

wpf - 从/向 SQL 保存和检索 RichTextBox.Document?

wpf - 在WPF Richtextbox中获取TextPointer的XY坐标

c# - 如果 WPF comboBox 中有相同的项目,则不会触发 SelectionChanged 事件

c# - 计算 Windows 文件夹大小的最快方法是什么?

c# - 访问从java到dotnet的请求

.net - ExpandoObject 到静态对象,然后再返回。跨越两个域

c# - Assembly.Load 和环境.CurrentDirectory

wpf - 如何在 UserControl 中显示 ObservableCollection<string>