Sven Borden
Sven Borden

Reputation: 1090

Set Autosized label to center c#

I have a Label in Windows Forms which has a specific text and size and has the property autosize set to true. I try to set his location to the center, but while using this code, it doesn't fit to the center of the form.

Label lab = new Label();
lab.Text = "blablabla";
lab.Font = new Font("Segoe UI", 32);
lab.Autosize = true;
lab.Location = new Point(ClientSize.Width / 2 - lab.Width / 2, 0);
this.Controls.Add(lab);

Upvotes: 1

Views: 1847

Answers (1)

Rahul
Rahul

Reputation: 77876

If it's Winform then set AutoSize to false lab.Autosize = false; along with that set the TextAlign property to MiddleCenter lab.TextAlign = MiddleCenter and choose Fill for the docking property.

Upvotes: 2

Related Questions