Sandy
Sandy

Reputation: 11687

Center Text align for labels

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?

Upvotes: 7

Views: 18277

Answers (3)

janbak
janbak

Reputation: 91

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).

Upvotes: 9

gbianchi
gbianchi

Reputation: 2138

You need to set the label autosize to true, put that label inside a frame (or a table cell), and make the anchor properties to none. That way it will be centered always.

Upvotes: 0

Ash Burlaczenko
Ash Burlaczenko

Reputation: 25435

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.

Upvotes: 5

Related Questions