如何使用多色彩的richtextbox如何使用、多色、richtextbox

2023-09-02 10:55:47 作者:丶最后一次颓废

我使用C#Windows Forms和我的richtextbox,我要的颜色一些文字为红色,一些绿色和一些黑色的。

I using C# windows forms and I have richtextbox and I want to color some text in red, some in green and some in black.

怎么办呢?图片附后。

How to do so? Image attached.

推荐答案

System.Windows.Forms.RichTextBox 已得到类型的属性颜色名称 selectionColor设置它获取或设置当前选择或插入点的文字颜色。您可以使用此属性来标记你的的RichTextBox 具体领域与您指定的颜色。

System.Windows.Forms.RichTextBox has got a property of type Color of the name SelectionColor which gets or sets the text color of the current selection or insertion point. You can use this property to mark specific fields in your RichTextBox with the colors you specify.

示例

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(字符串str)可以通过添加对象浏览器如果你想突出的的RichTextBox 中的文本给它的值

Notice that: You may avoid calculations by using RichTextBox.Find(string str) which can be added through Object Browser if you would like to highlight the text within the Lines in RichTextBox giving it's 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

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

Thanks, I hope you find this helpful :)