简体   繁体   English

如何为单个标签设置不同的原色?

[英]How to set different forecolor to single label?

lblOperationType.Text = "Text";
Label l1 = new Label();
int len = lblOperationType.Text.Length - 1;
string b = Convert.ToString(lblOperationType.Text.ToCharArray()[0]);
string a = lblOperationType.Text.Substring(1, len);
l1.Text = (b);
l1.ForeColor = Color.Red;
lblOperationType.Text = l1.Text + a;

Is this correct code? 这是正确的代码吗? I have to make a single label Text like 1st Letter should be red in color. 我必须制作一个标签,如“第一个字母”的文本应为红色。

No, in general (ie without custom rendering yourself) a label only has a single foreground colour. 不,通常(即您自己无需自定义渲染),标签仅具有单一前景色。 Assuming this is Windows Forms, it sounds like you might want a RichTextBox instead - that allows multiple colours, fonts etc within a single control. 假设这是Windows窗体,听起来您可能需要RichTextBox代替-在单个控件中允许多种颜色,字体等。

As an example: 举个例子:

using System;
using System.Drawing;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        var rtb = new RichTextBox {
            Text = "Test",
            ReadOnly = true
        };
        rtb.Select(1, 3);
        rtb.SelectionColor = Color.Red;
        rtb.DeselectAll();
        var form = new Form { Controls = { rtb } };
        Application.Run(form);
    }
}

That's not terribly nice code - it would be better to set the Rtf property directly, with the control codes necessary to set the colour, but I'm having a tricky time getting the exact format of the RTF right. 那不是非常好的代码-最好直接设置Rtf属性,并使用设置颜色所需的控制代码,但是我很难掌握正确的RTF格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM