0
votes
IDE = Visual Studio 2010  
Language = c# .net   
Technology = windows forms application  

Hi, i am having 3 labels

Label1 details:

label1.text = "Team1";  
label1.forecolor = Color.yellow; 

Label2 details:

label2.text = "Team1";  
label2.forecolor = Color.green;

Label3 details:

label3.text = "Team1";  
label3.forecolor = Color.grey;  

Now, What i am trying is this :

label4.text = "Match between " + label1.text+ " and " +label2.text+ " at " +label3.text         ;  

But the output of this i am getting all the labels in same color which is set to label4. and i want it to be displayed in different color.

I have tried this solution to put the different labels in form but it is getting overlapped.

so is there any way to concatenate labels..?

Any suggestions ?

4

4 Answers

2
votes

If you are inclined, you can create a UserControl and override its OnPaint function to draw these 3 chunks of text in different colors using GDI+. All it would take is a simple DrawString call.

e.Graphics.DrawString("LabelText", this.Font, Brushes.Red, new Point(100, 100));

You can then expose 3 public properties to control the 3 chunks of text and optionally 3 color properties to control chunk's color. Note that you need to create SolidBrush from those colors in OnPaint because that's what DrawString expects, like this:

using(SolidColorBrush br = new SolidColorBrush(mColor1)
    e.Graphics.DrawString("LabelText", this.Font, br, new Point(100, 100));

Full code:

public partial class MultiColorLabel : Label
{
    public MultiColorLabel()
    {
        InitializeComponent();
    }

    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string Text3 { get; set; }

    public Color Color1 { get; set; }
    public Color Color2 { get; set; }
    public Color Color3 { get; set; }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        var g = e.Graphics;

        e.Graphics.DrawString("Match between ", this.Font, Brushes.Black, 0, 0);
        var sz = g.MeasureString("Match between ", this.Font).Width;

        using (SolidBrush sb = new SolidBrush(Color1))
            e.Graphics.DrawString(Text1, this.Font, sb, sz, 0);

        sz += g.MeasureString(Text1, this.Font).Width;

        e.Graphics.DrawString(" and ", this.Font, Brushes.Black, sz, 0);
        sz += g.MeasureString(" and ", this.Font).Width;

        using (SolidBrush sb = new SolidBrush(Color2))
            e.Graphics.DrawString(Text2, this.Font, sb, sz, 0);

        sz += g.MeasureString(Text2, this.Font).Width;

        e.Graphics.DrawString(" at ", this.Font, Brushes.Black, sz, 0);
        sz += g.MeasureString(" at ", this.Font).Width;

        using (SolidBrush sb = new SolidBrush(Color3))
            e.Graphics.DrawString(Text3, this.Font, sb, sz, 0);
    }
}

Here's the snapshot:

enter image description here

0
votes

Label.Text is only return a string not any format. So all of text here will have the same color of label4. You need add your labels in a pannel control instead.

0
votes

Problem : you are just assigning the Text of other Label controls, so it only displays the Text but other properties like (ForeColor) wont be applied.

Solution : in my opinion i don't think that displaying multiple colurs for single Label String is possible directly.

So you can either develop your own User Control to dispaly Text in multiple controls.

or

You can use RichTextBox control.

-1
votes

Text` will not return a the whole label, it will only return the text(string) of that label. If you want to do what you wanted go and read some posts about Graphics.