Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.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部分的颜色#_C# - Fatal编程技术网

C# 如何更改标签c部分的颜色#

C# 如何更改标签c部分的颜色#,c#,C#,早上好 我想实现一个系统来搜索列表中的项目。 我编写了这需要的代码,但我遇到了一个问题 例如,我的标签文本如下: 河豚 我有一个字符串,其中包含以下内容: 波波塔 我希望我的标签是黑色的“Hi”,红色的“popota”,最后是黑色的“mus” 我在网上搜索了很多东西,真的有一段时间了,所以我找到了一个论坛 我希望您能帮助我:)标签控件本身不支持多种颜色,但RichTextBox控件支持多种颜色!您可以将其属性设置为类似于标签 例如,要使其看起来像标签,请执行以下操作: private void

早上好

我想实现一个系统来搜索列表中的项目。 我编写了这需要的代码,但我遇到了一个问题

例如,我的标签文本如下:

河豚

我有一个字符串,其中包含以下内容:

波波塔

我希望我的标签是黑色的“Hi”,红色的“popota”,最后是黑色的“mus

我在网上搜索了很多东西,真的有一段时间了,所以我找到了一个论坛


我希望您能帮助我:)

标签
控件本身不支持多种颜色,但
RichTextBox
控件支持多种颜色!您可以将其属性设置为类似于标签

例如,要使其看起来像标签,请执行以下操作:

private void Form1_Load(object sender, EventArgs e)
{
    // Make the RichTextBox look and behave like a Label control
    richTextBox1.BorderStyle = BorderStyle.None;
    richTextBox1.BackColor = System.Drawing.SystemColors.Control;
    richTextBox1.ReadOnly = true;
    richTextBox1.Text = "Hipopotamus";

    // I added a small, blank Label control to the form which I use to capture the Focus
    // from this control, so the user can't see the caret or select/highlight/edit text
    richTextBox1.GotFocus += (s, ea) => { lblHidden.Focus(); };
}
然后,通过设置“选择开始”和“长度”并更改选定颜色来突出显示搜索词的方法:

private void HighlightSearchText(string searchText, RichTextBox control)
{
    // Make all text black first
    control.SelectionStart = 0;
    control.SelectionLength = control.Text.Length;
    control.SelectionColor = System.Drawing.SystemColors.ControlText;

    // Return if search text isn't found
    var selStart = control.Text.IndexOf(searchText);
    if (selStart < 0 || searchText.Length == 0) return;

    // Otherwise, highlight the search text
    control.SelectionStart = selStart;
    control.SelectionLength = searchText.Length;
    control.SelectionColor = Color.Red;
}

Winforms、Wpf、Xamarin?如果使用Winforms,我不认为标签控件支持多色文本,而是让它看起来像一个标签。否则,请指定项目类型,以便我们知道您在说
Label
时具体指的是哪个类。您为此做了哪些尝试?关于这个->WPF->有很多资料,也许这就是你要找的:我当然是winform。我会试试richtextbox,谢谢你的想法。谢谢谢谢谢谢谢谢,再次感谢你提供了非常详细的答案,效果非常好!很少有人会得到这样的回应:(()()()()())())())()())()))())
private void txtSearch_TextChanged(object sender, EventArgs e)
{
    HighlightSearchText(txtSearch.Text, richTextBox1);
}