I am doing a college project and need to display client status in form of coloured * by using an enum with flags...Now i know how the enum works but the problem is I need to show more than one * within the same field (lable or anything else) but each * with a different colour according to the creteria... Now i cant use label.forecolour cause that sets the whole lables' colour... Can anyone give me some advise?

Recommended Answers

All 11 Replies

Is it essental to use a single label? Labels don't have support for multicoloured text, so you can either create a custom user control or override the paint event to draw the *'s in different colours yourself.

It would be easier to have different labels that you simply set to visible = true to show different colours.

Thnx and yes it is essential to use only one label. I'll have a look into the paint event although i have no clue to how to use it or even what it is. Tnx Again!

//attach event handler to Paint event in designer
this.label1.Paint += new System.Windows.Forms.PaintEventHandler(this.label1_Paint);

//then handle the event in code
private void label1_Paint(object sender, PaintEventArgs e)
{
            label1.Text = "";
            label1.AutoSize = false;
            int width = (int)e.Graphics.MeasureString("*", f).Width;
            Font f = ((Label)sender).Font;

            label1.Width = (width * 3);

            Rectangle rect = e.ClipRectangle;
            e.Graphics.DrawString("*", f, Brushes.Red, rect);

            rect.X += width;
            rect.Width -= width;
            e.Graphics.DrawString("*", f, Brushes.Blue, rect);

            rect.X += width;
            rect.Width -= width;
            e.Graphics.DrawString("*", f, Brushes.Yellow, rect); }

That prints three asterix's...should you give you a starting point.

commented: Nice :) +14
commented: Glad to see another C# Solver on board! +17
commented: yeah especially after serkan left +11

According to the criteria Just try dynamic label control like this,

label local_label = new label();
label.forecolor = red;
label.ID = creating with that particular enum value.

Thx
Chandru
SNIP

Very nice code Ryshad!
You only should swap line 9 and 10 otherwise f is used before it is defined.

well spotted :p
I originally used it further down, but moved it up to measure the string to set the label width and forgot to move the assignment with it : /

And thanks, i've been helped out twice now, wanted to give something back to the community :D

Tnx for all the replys so far...haven't got it right though yet...but will let know when i got it working. i really apreciate all the input, tnx.

if your still havin problems, post your code so far and we can point you in the right direction :)

Tnx for all the input....i misunderstood the question myself...They told me today the single field was intended for the database(which i already had)....sorry for the troubles....i'm using seperate labels now for each status, and tnx again!

No worries, glad to have helped :)

how to use multiple color in single label window form c#.It is use multiple text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.