Center Text align for labels

17,309

Solution 1

You will also need to set the width of the label to the length of your longest text, or just to the width of the form.

Solution 2

You can even do better: set the label Autosize OFF, and manually stretch the label in the designer to fill the area that's needed to show the largest text value. Now if you set the TextAlign to MiddleCenter, you get the effect you want (even in the designer, so you can check it by setting a value in the Text property).

Share:
17,309
Sandy
Author by

Sandy

Enthusiast in technology world and fascinated by new entrepreneurs. Strongly believe in innovation and creativity and looking forward to be a part of it.

Updated on June 23, 2022

Comments

  • Sandy
    Sandy almost 2 years

    I have a label exactly in center of a form with text "It is my Visual Studio application". Now, I have a button click event that changes the text to "My application". But the problem is the alignment gets disturbed as the length of changed text is different from previous one. I tried TextAlign option of label to MiddleCenter. but no luck. Can any one help?

  • Sandy
    Sandy over 12 years
    But will it be a good solution, i have number of lables in different places with different alignment property and placing them in each panel will be a bit difficult. Well I know this method already but looking for a better solution. Still many thanks
  • gbianchi
    gbianchi over 12 years
    So, what is exactly your problem? if not centering the label, can you explain it further? other solutions could involve calculating the length of the label and move it according to the new width.
  • Sandy
    Sandy over 12 years
    and obviously autosize to false before that. Thanks a lot.