You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中如何修改Label控件部分文本的颜色与字号

How to Format Partial Text in WinForms Label (Color & Font Size)

Hey there! If you're working with WinForms, you probably noticed the standard Label control doesn't natively support rich text formatting—meaning you can't change color/size for just parts of the text out of the box. But don't worry, we've got two straightforward solutions to get this done.

Method 1: Use a RichTextBox (Quick & Easy)

We can repurpose a RichTextBox to look and act like a Label, while leveraging its built-in rich text support. Just tweak its properties to match your Label's style, then apply formatting to specific text segments.

Here's how to adapt your existing code for this approach:

// For label3 equivalent (using RichTextBox)
RichTextBox rtbx3 = new RichTextBox();
rtbx3.Width = pictureBox1.Width * 2 - 100;
rtbx3.Top = pictureBox1.Top + pictureBox1.Height - 50;
rtbx3.Left = pictureBox1.Left - 75;
rtbx3.Height = 200;
rtbx3.BorderStyle = BorderStyle.None; // Remove border to look like Label
rtbx3.BackColor = this.BackColor; // Match form background
rtbx3.ReadOnly = true; // Prevent editing
rtbx3.Text = "Regele Carol al II-lea(1930-1940) si fiul sau Mihai I(9 ani) cu prilejul proclamarii principelui Carol al II-lea, Rege al Romaniei, de catre Parlamewntul de la Bucuresti, la 8 Iunie 1930";

// Format the year segments (1930-1940) and 8 Iunie 1930
int yearStart1 = rtbx3.Text.IndexOf("1930-1940");
if (yearStart1 != -1)
{
    rtbx3.SelectionStart = yearStart1;
    rtbx3.SelectionLength = "1930-1940".Length;
    rtbx3.SelectionColor = Color.Blue;
    rtbx3.SelectionFont = new Font(rtbx3.Font.FontFamily, rtbx3.Font.Size + 2, FontStyle.Bold); // Larger size
}

int yearStart2 = rtbx3.Text.IndexOf("8 Iunie 1930");
if (yearStart2 != -1)
{
    rtbx3.SelectionStart = yearStart2;
    rtbx3.SelectionLength = "8 Iunie 1930".Length;
    rtbx3.SelectionColor = Color.Blue;
    rtbx3.SelectionFont = new Font(rtbx3.Font.FontFamily, rtbx3.Font.Size + 2, FontStyle.Bold);
}

this.Controls.Add(rtbx3);

// Repeat similar steps for label4 equivalent
RichTextBox rtbx4 = new RichTextBox();
rtbx4.Width = pictureBox2.Width * 2 - 75;
rtbx4.Top = pictureBox2.Top + pictureBox2.Height - 50;
rtbx4.Left = pictureBox2.Left - 75;
rtbx4.Height = 150;
rtbx4.BorderStyle = BorderStyle.None;
rtbx4.BackColor = this.BackColor;
rtbx4.ReadOnly = true;
rtbx4.Text = "Clasa de elevi cu Mihai I ce poarta titlul de: 'Mare Voievod de ALBA IULIA' in 1937, de Craciun 25 XII";

// Format "1937" and "25 XII"
int yearStart3 = rtbx4.Text.IndexOf("1937");
if (yearStart3 != -1)
{
    rtbx4.SelectionStart = yearStart3;
    rtbx4.SelectionLength = "1937".Length;
    rtbx4.SelectionColor = Color.Blue;
    rtbx4.SelectionFont = new Font(rtbx4.Font.FontFamily, rtbx4.Font.Size + 2, FontStyle.Bold);
}

int dateStart = rtbx4.Text.IndexOf("25 XII");
if (dateStart != -1)
{
    rtbx4.SelectionStart = dateStart;
    rtbx4.SelectionLength = "25 XII".Length;
    rtbx4.SelectionColor = Color.Blue;
    rtbx4.SelectionFont = new Font(rtbx4.Font.FontFamily, rtbx4.Font.Size + 2, FontStyle.Bold);
}

this.Controls.Add(rtbx4);

Method 2: Custom Drawn Label (More Control, Native Label Feel)

If you prefer to stick with a Label-like control without using RichTextBox, you can create a custom Label that overrides the OnPaint method to draw text with different formats manually.

First, create the custom control:

public class FormattedLabel : Label
{
    // Define segments with custom formatting
    public List<TextSegment> FormattedSegments { get; set; } = new List<TextSegment>();

    protected override void OnPaint(PaintEventArgs e)
    {
        // Draw default text first (unformatted parts)
        e.Graphics.Clear(this.BackColor);
        var currentPosition = new PointF(0, 0);
        string remainingText = this.Text;

        foreach (var segment in FormattedSegments)
        {
            // Find the segment in the remaining text
            int index = remainingText.IndexOf(segment.Text);
            if (index == -1) continue;

            // Draw the unformatted text before the segment
            if (index > 0)
            {
                string preText = remainingText.Substring(0, index);
                e.Graphics.DrawString(preText, this.Font, new SolidBrush(this.ForeColor), currentPosition);
                currentPosition.X += e.Graphics.MeasureString(preText, this.Font).Width;
            }

            // Draw the formatted segment
            e.Graphics.DrawString(segment.Text, segment.Font, new SolidBrush(segment.Color), currentPosition);
            currentPosition.X += e.Graphics.MeasureString(segment.Text, segment.Font).Width;

            // Update remaining text
            remainingText = remainingText.Substring(index + segment.Text.Length);
        }

        // Draw any remaining unformatted text
        if (!string.IsNullOrEmpty(remainingText))
        {
            e.Graphics.DrawString(remainingText, this.Font, new SolidBrush(this.ForeColor), currentPosition);
        }
    }

    public class TextSegment
    {
        public string Text { get; set; }
        public Font Font { get; set; }
        public Color Color { get; set; }
    }
}

Then use it in your form code:

// For label3 equivalent
FormattedLabel lbl3 = new FormattedLabel();
lbl3.Width = pictureBox1.Width * 2 - 100;
lbl3.Top = pictureBox1.Top + pictureBox1.Height - 50;
lbl3.Left = pictureBox1.Left - 75;
lbl3.Height = 200;
lbl3.Text = "Regele Carol al II-lea(1930-1940) si fiul sau Mihai I(9 ani) cu prilejul proclamarii principelui Carol al II-lea, Rege al Romaniei, de catre Parlamewntul de la Bucuresti, la 8 Iunie 1930";

// Add formatted segments
var largerBlueFont = new Font(lbl3.Font.FontFamily, lbl3.Font.Size + 2, FontStyle.Bold);
lbl3.FormattedSegments.Add(new FormattedLabel.TextSegment
{
    Text = "1930-1940",
    Font = largerBlueFont,
    Color = Color.Blue
});
lbl3.FormattedSegments.Add(new FormattedLabel.TextSegment
{
    Text = "8 Iunie 1930",
    Font = largerBlueFont,
    Color = Color.Blue
});

this.Controls.Add(lbl3);

// Repeat for label4 equivalent
FormattedLabel lbl4 = new FormattedLabel();
lbl4.Width = pictureBox2.Width * 2 - 75;
lbl4.Top = pictureBox2.Top + pictureBox2.Height - 50;
lbl4.Left = pictureBox2.Left - 75;
lbl4.Height = 150;
lbl4.Text = "Clasa de elevi cu Mihai I ce poarta titlul de: 'Mare Voievod de ALBA IULIA' in 1937, de Craciun 25 XII";

lbl4.FormattedSegments.Add(new FormattedLabel.TextSegment
{
    Text = "1937",
    Font = largerBlueFont,
    Color = Color.Blue
});
lbl4.FormattedSegments.Add(new FormattedLabel.TextSegment
{
    Text = "25 XII",
    Font = largerBlueFont,
    Color = Color.Blue
});

this.Controls.Add(lbl4);

Quick Notes:

  • For the RichTextBox method, double-check that BackColor matches your form's background so it blends in seamlessly.
  • The custom FormattedLabel gives you full control over text rendering, making it ideal if you need consistent behavior across multiple labels in your app.

内容的提问来源于stack exchange,提问作者Abdul Asam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:38:45