Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one Label with Text : Project .....In this text i need pro in one color and ject in another color ...

I need this in Windows Application...
Posted

There is no native control in .NET that does this. Your best bet is to write your own UserControl. 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

C#
public void RenderRainbowText(string Text, PictureBox pb)
{
 pb.Image = new Bitmap(pb.Width, pb.Height);
 using (Graphics g = Graphics.FromImage(pb.Image))
 {
    SolidBrush brush = new SolidBrush(Color.White);
    g.FillRectangle(brush, 0, 0,
    pb.Image.Width, pb.Image.Height);
    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++)
    {
     g.DrawString(chunks[i], pb.Font, brushes[i], x, 0);
     x += (g.MeasureString(chunks[i], pb.Font)).Width;
     if (i < (chunks.Length - 1))
     {
        g.DrawString(",", pb.Font, brush, x, 0);
        x += (g.MeasureString(",", pb.Font)).Width;
     }
    }
 }
}
 
Share this answer
 
Why not simply place this on two separate labels aligned properly? If it is a case where the split position of the text is dynamic, you can do as following:
1. Add a FlowLayoutPanel and add the two labels to that panel
2. Add a Panel and add the two labels to that panel. Set the "AutoSize" property to true. Then dock the two labels to "left"

These two methods will allow you to add/remove text to the first label and it will automatically re-size while moving the second label along with it.

Hope I did not misunderstand your question and this helps :)
 
Share this answer
 
Comments
itsureshuk 29-Feb-12 2:10am    
in the same label i need different colors for text

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900