Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 改变子串的颜色_C#_String_Winforms - Fatal编程技术网

C# 改变子串的颜色

C# 改变子串的颜色,c#,string,winforms,C#,String,Winforms,这可能吗?例如,如果我有一个标签: lblsentence.Text = "Blue is my favourite colour, and Red is my least favourite" 我可以将“蓝色”和“红色”更改为不同的颜色,并将标签文本的其余部分保留为默认值(黑色)?请尝试下面的操作 用于网络 Type colorType = typeof(System.Drawing.Color); // We take only static property to avoid prope

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

lblsentence.Text = "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();
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;
Type colorType=typeof(System.Drawing.Color);
//我们只使用静态属性来避免像Name、IsSystemColor。。。
System.Reflection.PropertyInfo[]propinfo=colorType.GetProperties(System.Reflection.bindingsflags.Static | System.Reflection.bindingsflags.DeclaredOnly | System.Reflection.bindingsflags.Public);
string[]Colors=propinfo.Select(m=>m.Name.ToArray();
string str=lblsentence.Text;
foreach(颜色中的字符串颜色)
{
如果(str.Contains(颜色))
{
字符串replaceColor=“”+颜色+”;
str=str.Replace(颜色,replaceColor);
}
}
lblsentence.Text=str;
适用于Windows窗体

我们可以在Win窗体中使用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;
string str=“蓝色是我最喜欢的颜色,红色是我最不喜欢的颜色”;
类型colorType=typeof(System.Drawing.Color);
//我们只使用静态属性来避免像Name、IsSystemColor。。。
System.Reflection.PropertyInfo[]propinfo=colorType.GetProperties(System.Reflection.bindingsflags.Static | System.Reflection.bindingsflags.DeclaredOnly | System.Reflection.bindingsflags.Public);
string[]Colors=propinfo.Select(m=>m.Name.ToArray();
foreach(颜色中的字符串颜色)
{
如果(str.Contains(颜色))
{
字符串replaceColor=“”+颜色+”;
str=str.Replace(颜色,replaceColor);
}
}            
webBrowser1.DocumentText=str;

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


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

,下面是一个富文本框控件示例

        // 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;

你为什么不在表格上画你自己的文字呢
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));    
}

字符串是无色的,您的意思是要更改标签的颜色吗?您使用的是哪种API?窗口窗体?WPF?银灯?Asp.NET?(当然,我完全同意@Sayse)没有更多的上下文,这有点像问你是否可以将8变成绿色而不是粉色。我想你的意思是改变分配给这个字符串的html控件的颜色?你可以使用一个富文本框,并设置它的属性,使其充当标签。RTB支持文本中的多种颜色。@Shell:对于WinForms,您可以使用WebBrowser控件而不是Label,答案也可以修改。背景颜色取决于要求。如果我的表格是白色的,它看起来会很完美。除此之外,问题中没有提到它。查看此()了解更多信息,更好地发挥建设性而不是破坏性。@Shell:我们不是来开发整个应用程序的,我们是来给人们一个想法,让他们知道如何通过我们的解决方案解决问题的。如果符合他们的要求,允许他们采用。你不觉得吗?这不是通用的,如果字符串中有黄色或绿色怎么办?OP没有提到这一点。顺便说一句,我们可以使用
color.FromName(stringcolorname)
方法从颜色名称中获取颜色。我们应该以一般方式回答,OP刚才给出了一个彩色字符串的示例。你能把它变成普通的吗?