-2

Can label1 text&property and label2 text&property become one and display it in label3 and added text = ? because what I am using now is using label1 and label2 putting side by side.

enter image description here

Tell me if there's another approach

Ps: I define the color in a database like red or blue.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
chopperfield
  • 559
  • 1
  • 7
  • 25

3 Answers3

1

Your can combine the text content like this:

label3.Text = label1.Text + " = " + label2.Text;

But you will loose the different colours. This is unfortunately not possible. For more details check this answer

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
1

Use string.format to combine the 2 labels texts together.

label3.Text = string.Format("{0}={1}", label1.Text, label2.Text);
Wheels73
  • 2,850
  • 1
  • 11
  • 20
0

you can write your own text image your label3. like here

and.

firstly set label3 AutoSize = false and set size .

    // Add this lines to InitializeComponent() in yourform.Designer.cs
    this.label1.TextChanged += new System.EventHandler(this.label_TextChanged);
    this.label2.TextChanged += new System.EventHandler(this.label_TextChanged);


    // this is label1 and label2 TextCahanged Event
    private void label_TextChanged(object sender, EventArgs e)
    {
        SetMultiColorText(string.Format("{0} = {1}", label1.Text, label2.Text),label3);
    }

// this method set multi color image text for label(paramter lb)
    public void SetMultiColorText(string Text, Label lb)
    {
        lb.Text = "";
        // PictureBox needs an image to draw on
        lb.Image = new Bitmap(lb.Width, lb.Height);
        using (Graphics g = Graphics.FromImage(lb.Image))
        {
            
            
            SolidBrush brush = new SolidBrush(Form.DefaultBackColor);
            g.FillRectangle(brush, 0, 0,
                lb.Image.Width, lb.Image.Height);
            
            string[] chunks = Text.Split('=');
            brush = new SolidBrush(Color.Black);

            // you can get this colors from label1 and label2 colors... or from db.. or an other where you want
            SolidBrush[] brushes = new SolidBrush[] { 
        new SolidBrush(Color.Black),
        new SolidBrush(Color.Red) };
            float x = 0;
            for (int i = 0; i < chunks.Length; i++)
            {
                // draw text in whatever color
                g.DrawString(chunks[i], lb.Font, brushes[i], x, 0);
                // measure text and advance x
                x += (g.MeasureString(chunks[i], lb.Font)).Width;
                // draw the comma back in, in black
                if (i < (chunks.Length - 1))
                {
                    g.DrawString("=", lb.Font, brush, x, 0);
                    x += (g.MeasureString(",", lb.Font)).Width;
                }
            }
        }
    }
miken32
  • 42,008
  • 16
  • 111
  • 154
levent
  • 3,464
  • 1
  • 12
  • 22