C# 如何在富文本框中使用多色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13220856/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:47:19  来源:igfitidea点击:

How to use multi color in richtextbox

c#.netrichtextbox

提问by Billie

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.

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

How to do so? Image attached.

怎么做?附上图片。

enter image description here

在此处输入图片说明

采纳答案by Picrofo Software

System.Windows.Forms.RichTextBoxhas got a property of type Colorof the name SelectionColorwhich gets or sets the text color of the current selection or insertion point. You can use this property to mark specific fields in your RichTextBoxwith the colors you specify.

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

Example

例子

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

Notice that: You may avoid calculations by using RichTextBox.Find(string str)which can be added through Object Browserif you would like to highlight the text within the Linesin RichTextBoxgiving it's value

注意:您可能避免计算通过使用RichTextBox.Find(string str)可通过添加Object Browser,如果你想突出内的文本LinesRichTextBox赋予它的价值

Example

例子

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

谢谢,
我希望你觉得这有帮助:)

回答by Mark Kram

I found this extension method that gives you the ability to change the color of the string as well as inserting a newline value:

我发现这个扩展方法使您能够更改字符串的颜色以及插入换行符值:

    public static void AppendText(this RichTextBox box, string text, Color color, bool AddNewLine = false)
    {
        if (AddNewLine)
        {
            text += Environment.NewLine;
        }

        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }

回答by Boobalan

you can use Run object to change the color at runtime

您可以使用 Run 对象在运行时更改颜色

private Run GetForegroundColor(string strInformation, Brush color)
        {
            Run noramlRun = new Run(strInformation);
            noramlRun.Foreground = color;
            return noramlRun;
        }

for more complex scenario like change the color based on requirement then visit blow link

对于更复杂的场景,如根据需求更改颜色,然后访问吹链接

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/customwpfrichtextboxwithcolorchangeandhighlightfunctionality