Centering the Title on the form

Dhruva

Member
Joined
Sep 23, 2004
Messages
12
Programming Experience
Beginner
Hi , I am defing a label for my form title which is dynamic. I set Autosize propery to true so that all the contents can fit into the label. But the question is how do I center the Label/Title to my form. What is the property or code by which I can place my label at the center of the form..
This is required as my title can be changed during run time.
Any help on this one ?
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
The easiest way would be to set the TextAlign to one of the center horizontally aligned values (either TopCenter, MiddleCenter, or BottomCenter), set Autosize property to false, either center the label or have it the entire width of the form, and if the form is resizable, anchor it to the left and right.
Another way is to use the form's Resize event and set the label's Left property based on the simple equation:
VB.NET:
Label1.Left = (Me.Width - Label1.Width) / 2
 
Top Bottom