Multiple colors in a C# .NET label

后端 未结 11 1619
無奈伤痛
無奈伤痛 2020-11-27 03:54

I\'m looking for a way to display multiple colors in a single C#/.NET label. E.g the label is displaying a series of csv separated values that each take on a color dependin

相关标签:
11条回答
  • 2020-11-27 04:01

    There is no native support for this; you will either have to use multiple labels or find a 3rd-party control that will provide this functionality.

    0 讨论(0)
  • 2020-11-27 04:03

    You could try using a RichTextBox so that you can get multiple colors for the string and then make it read only and remove the border. Change the background color to the same as the Form it is on and you might get away with it.

    0 讨论(0)
  • 2020-11-27 04:06

    You can simply use multiple labels. Set the font properties you want and then use the left. top and width properties to display the words you want displayed differently. This is assuming you are using windows forms.

    0 讨论(0)
  • 2020-11-27 04:12

    There is no native control in .NET that does this. Your best bet is to write your own UserControl (call it RainbowLabel or something). Normally you would have a custom label control inherit directly from Label, but since you can't get multi-colored text in one label, you would just inherit from UserControl.

    For rendering the text, your UserControl could split the text on commas and then dynamically load a differently-colored Label for each chunk. A better way, however, would be to render the text directly onto your UserControl using the DrawString and MeasureString methods in the Graphics namespace.

    Writing UserControls in .NET is really not difficult, and this kind of unusual problem is exactly what custom UserControls are for.

    Update: here's a simple method you can use for rendering the multi-colored text on a PictureBox:

    public void RenderRainbowText(string Text, PictureBox pb)
    {
        // PictureBox needs an image to draw on
        pb.Image = new Bitmap(pb.Width, pb.Height);
        using (Graphics g = Graphics.FromImage(pb.Image))
        {
            // create all-white background for drawing
            SolidBrush brush = new SolidBrush(Color.White);
            g.FillRectangle(brush, 0, 0,
                pb.Image.Width, pb.Image.Height);
            // draw comma-delimited elements in multiple colors
            string[] chunks = Text.Split(',');
            brush = new SolidBrush(Color.Black);
            SolidBrush[] brushes = new SolidBrush[] { 
                new SolidBrush(Color.Red),
                new SolidBrush(Color.Green),
                new SolidBrush(Color.Blue),
                new SolidBrush(Color.Purple) };
            float x = 0;
            for (int i = 0; i < chunks.Length; i++)
            {
                // draw text in whatever color
                g.DrawString(chunks[i], pb.Font, brushes[i], x, 0);
                // measure text and advance x
                x += (g.MeasureString(chunks[i], pb.Font)).Width;
                // draw the comma back in, in black
                if (i < (chunks.Length - 1))
                {
                    g.DrawString(",", pb.Font, brush, x, 0);
                    x += (g.MeasureString(",", pb.Font)).Width;
                }
            }
        }
    }
    

    Obviously this will break if you have more than 4 comma-delimited elements in your text, but you get the idea. Also, there appears to be a small glitch in MeasureString that makes it return a width that is a couple pixels wider than necessary, so the multi-colored string appears stretched out - you might want to tweak that part.

    It should be straightforward to modify this code for a UserControl.

    Note: TextRenderer is a better class to use for drawing and measuring strings, since it uses ints. Graphics.DrawString and .MeasureString use floats, so you'll get off-by-a-pixel errors here and there.

    Update: Forget about using TextRenderer. It is dog slow.

    0 讨论(0)
  • 2020-11-27 04:16

    If you are building your Windows app for people with XP and up, you can use WPF. Even if it's a Windows Forms app, you can add a WPF UserControl.

    I would then use a Label, and set the "Foreground" property to be a gradient of colors.

    Or, in Windows Forms (no WPF), you could just use a "Flow Panel", and then in a for loop add multiple Labels as segments of your sentense... they will all "flow" together as if it was one label.

    0 讨论(0)
  • 2020-11-27 04:16

    Slightly off topic ... You could check also:

    • generate html color table
    • model colors in sql
    • the result
    0 讨论(0)
提交回复
热议问题