更改子串的颜色

时间:2014-07-29 07:11:48

标签: c# string winforms

这可能吗?例如,如果我有一个标签:

lblsentence.Text = "Blue is my favourite colour, and Red is my least favourite"

我可以将"Blue""Red"更改为不同的颜色,并将标签文本的其余部分保留为默认值(黑色)吗?

4 个答案:

答案 0 :(得分:2)

请尝试以下。

for Web

Type colorType = typeof(System.Drawing.Color);
// We take only static property to avoid properties like Name, IsSystemColor ...
System.Reflection.PropertyInfo[] propInfos = colorType.GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public);
string[] Colors = propInfos.Select(m => m.Name).ToArray();
string str = lblsentence.Text;
foreach(string color in Colors)
{
    if(str.Contains(color))
    {
        string replaceColor = "<span style='color:" + color + "'>" + color + "</span>";
        str = str.Replace(color, replaceColor);
    }
}
lblsentence.Text = str;

适用于Windows窗体

我们可以在Win-Forms而不是Label控件的情况下使用WebBrowser控件。

string str = "Blue is my favourite colour, and Red is my least favourite";
Type colorType = typeof(System.Drawing.Color);
// We take only static property to avoid properties like Name, IsSystemColor ...
System.Reflection.PropertyInfo[] propInfos = colorType.GetProperties(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public);
string[] Colors = propInfos.Select(m => m.Name).ToArray();

foreach (string color in Colors)
{
    if (str.Contains(color))
    {
        string replaceColor = "<span style='color:" + color + "'>" + color + "</span>";
        str = str.Replace(color, replaceColor);
    }
}            
webBrowser1.DocumentText = str;

答案 1 :(得分:2)

以下是富文本框控件的示例

        // set the selection at the end of the box and set selection to 0
        richTextBox1.SelectionStart = richTextBox1.SelectionLength;
        richTextBox1.SelectionLength = 0;


        richTextBox1.SelectionColor = Color.Blue;
        richTextBox1.AppendText("hello ");

        richTextBox1.SelectionColor = Color.Red;
        richTextBox1.AppendText("World");

        // set back the default color
        richTextBox1.SelectionColor = richTextBox1.ForeColor;

答案 2 :(得分:0)

如前所述,Wiforms标签不支持多种前景色。您需要RichTextBox或自定义控件。

我建议您使用HtmlRenderer库,它提供您可以使用的HtmlLabel控件。您所要做的就是将文本转换为有效的Html

答案 3 :(得分:0)

为什么不在表单上绘制自己的文字。 e.Graphics.DrawString()会帮助你。

private void Form_Paint(object sender, PaintEventArgs e)
{
    Font font = this.Font;

    int iLocation = 10;

    e.Graphics.DrawString("Blue", font, Brushes.Blue, new PointF(iLocation, 100));
    iLocation += e.Graphics.MeasureString("Blue", font) + 5;    

    e.Graphics.DrawString(" is my favourite colour, and ", font, Brushes.Black, new PointF(iLocation, 100));
    iLocation += e.Graphics.MeasureString(" is my favourite colour, and ", font) + 5;

    e.Graphics.DrawString("Red", font, Brushes.Red, new PointF(iLocation, 100));
    iLocation += e.Graphics.MeasureString("Red", font) + 5;

    e.Graphics.DrawString(" is my least favourite", font, Brushes.Black, new PointF(iLocation, 100));    
}