set label position based on neighbor label

10,125

Solution 1

You need to use label1.Right instead:

label2.Location = new Point(label1.Right, label2.Top);

Solution 2

You can use a FlowLayoutPanel which automatically positions its child controls next to each other.

+-------------------------------------------+
|FlowLayoutPanel                            |
| +------+ +------+                         |
| |Label1| |Label2|                         |
| +------+ +------+                         |
+-------------------------------------------+

 

+-------------------------------------------+
|FlowLayoutPanel                            |
| +--------------------+ +------+           |
| |LabelWithLotsOfText1| |Label2|           |
| +--------------------+ +------+           |
+-------------------------------------------+
Share:
10,125

Related videos on Youtube

The Mask
Author by

The Mask

Updated on June 04, 2022

Comments

  • The Mask
    The Mask about 2 years

    supposing that I have two labels with variables text.

    For example:

    label1.Text = "foo";
    label2.Text = "baa"; 
    

    Form output:

    ________________
    |               |    
    |foo baa        | 
    |______________ |
    

    other example:

    label1.Text = "fooooo";
    label2.Text = "baaaa"; 
    

    Form output:

        ________________
        |               |    
        |fooooo baaaa   | 
        |______________ |
    

    I tried it:

      label2.Location = new Point
                {
                    X = label1.Location.X + label2.Location.X,
                    Y = label1.Location.Y
                };
    

    I imagined that the space was sufficient. But label2 hide label1 if the text is larger.