Centering controls within a form in winforms
c#, center, label, winforms
Solution
This is happening because you are using the width of your label BEFORE you add it to the form's Controls.
However, the width of an autosized label is calculated AFTER it is added to the list of controls. Before that, if you look at the width it will be some fixed default value such as 100.
You can fix it by rearranging your code so that you adjust the label's position AFTER you've added it to the form's controls.
private void InitializeComponent(string str)
{
this.BackColor = Color.LightGray;
this.Text = str;
//this.FormBorderStyle = FormBorderStyle.Sizable;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.StartPosition = FormStartPosition.CenterScreen;
Label myLabel = new Label();
myLabel.Text = str;
myLabel.ForeColor = Color.Red;
myLabel.AutoSize = true;
Label myLabel2 = new Label();
myLabel2.Text = str;
myLabel2.ForeColor = Color.Blue;
myLabel2.AutoSize = false;
myLabel2.Dock = DockStyle.Fill;
myLabel2.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(myLabel);
this.Controls.Add(myLabel2);
myLabel.Left = (this.ClientSize.Width - myLabel.Width) / 2;
myLabel.Top = (this.ClientSize.Height - myLabel.Height) / 2;
}
Problem
I am learning C# and as a part of exercise from book I have to center label within a form. It doesn't matter if the form is sizeable or not. I found here - on stackoverflow - as well on some other places different solutions, and I narrowed it to two. But it seems, that albeit quite popular solutions they don't yield same result. It looks like that method 1 ``` myLabel.Left = (this.ClientSize.Width - myLabel.Width) / 2; myLabel.Top = (this.ClientSize.Height - myLabel.Height) / 2; ``` will produce label centered slightly left side and up offset from centre, and that method 2 ``` myLabel2.Dock = DockStyle.Fill; myLabel2.TextAlign = ContentAlignment.MiddleCenter; ``` will make it perfectly aligned in middle of form. Now, my question is why there is difference, or to put in other words, why is there left side-up offset in method 1 ? Whole code is as given below: ``` //Exercise 16.1 //------------------- using System.Drawing; using System.Windows.Forms; public class frmApp : Form { public frmApp(string str) { InitializeComponent(str); } private void InitializeComponent(string str) { this.BackColor = Color.LightGray; this.Text = str; //this.FormBorderStyle = FormBorderStyle.Sizable; this.FormBorderStyle = FormBorderStyle.FixedSingle; this.StartPosition = FormStartPosition.CenterScreen; Label myLabel = new Label(); myLabel.Text = str; myLabel.ForeColor = Color.Red; myLabel.AutoSize = true; myLabel.Left = (this.ClientSize.Width - myLabel.Width) / 2; myLabel.Top = (this.ClientSize.Height - myLabel.Height) / 2; Label myLabel2 = new Label(); myLabel2.Text = str; myLabel2.ForeColor = Color.Blue; myLabel2.AutoSize = false; myLabel2.Dock = DockStyle.Fill; myLabel2.TextAlign = ContentAlignment.MiddleCenter; this.Controls.Add(myLabel); this.Controls.Add(myLabel2); } public static void Main() { Application.Run(new frmApp("Hello World!")); } } ```