[英]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;
这是正确的代码吗? 我必须制作一个标签,如“第一个字母”的文本应为红色。
不,通常(即您自己无需自定义渲染),标签仅具有单一前景色。 假设这是Windows窗体,听起来您可能需要RichTextBox
代替-在单个控件中允许多种颜色,字体等。
举个例子:
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);
}
}
那不是非常好的代码-最好直接设置Rtf
属性,并使用设置颜色所需的控制代码,但是我很难掌握正确的RTF格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.