Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple colors in a C# .NET label

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 depending on a bucket they fall into. I would prefer not to use multiple labels, as the values are variable length and I don't want to play with dynamic layouts. Is there a native support for this?

like image 444
Cory Avatar asked Nov 09 '08 11:11

Cory


People also ask

How many colors are there in C language?

Total number of colors available are 16. Number of available colors depends on current graphics mode and driver. For example, setcolor(RED) or setcolor(4) changes the current drawing color to RED. Remember that default drawing color is WHITE.

What is Textcolor in C?

Function textcolor is used to change the color of drawing text in C programsTurbo C compiler only. Declaration: void textcolor(int color); where color is an integer variable. For example, 0 means BLACK color, 1 means BLUE, 2 means GREEN and soon.

Are there more than 3 colors?

Three Primary Colors (Ps): Red, Yellow, Blue. Three Secondary Colors (S'): Orange, Green, Violet. Six Tertiary Colors (Ts): Red-Orange, Yellow-Orange, Yellow-Green, Blue-Green, Blue-Violet, Red-Violet, which are formed by mixing a primary with a secondary.


1 Answers

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.

like image 125
MusiGenesis Avatar answered Sep 17 '22 22:09

MusiGenesis