locked
Setting dynamic label position RRS feed

  • Question

  • I'm trying to dynamically set Label B location depending on another Label A location and width then data binding is done:
    This is how I do it:
    this.LabelB.Location = new Point(LabelA.Location.X + LabelA.Width + 10, this.LabelB.Location.Y);

    The problem is, that both of labels has bigger font applied to them - Tahoma 15px and when I set location like that, it probably ignores the font and it's size, because Label B is still behind Label A.
    Wednesday, November 21, 2007 10:31 AM

Answers

  • What you need is to use the TextRenderer class, which gives you the ability to calculate the width that the label a is going to 'take':

     

    System.Windows.Forms.Label l1;

    System.Windows.Forms.Label l2;

    System.Drawing.Size size = TextRenderer.MeasureText(l1.Text, l1.Font);

    l2.Location = new System.Drawing.Point(l1.Location.X + size.Width + 10,

    l2.Location.Y);

     

    Wednesday, November 21, 2007 2:54 PM

All replies

  • What you need is to use the TextRenderer class, which gives you the ability to calculate the width that the label a is going to 'take':

     

    System.Windows.Forms.Label l1;

    System.Windows.Forms.Label l2;

    System.Drawing.Size size = TextRenderer.MeasureText(l1.Text, l1.Font);

    l2.Location = new System.Drawing.Point(l1.Location.X + size.Width + 10,

    l2.Location.Y);

     

    Wednesday, November 21, 2007 2:54 PM
  • Thank you Wink
    Wednesday, November 21, 2007 5:03 PM