AllTheTimeWorld.com

csharp arithmeticCentering a control on the form

Centering a control on the form

The control is centered when the form is resized

Centering a control on the form

The control is centered when the form is resized


Text of video
Review the video on changing the location first.
private void Form1_Resize(object sender, EventArgs e)
{
   //Center the label when the form is resized
   int whiteSpace = this.Width - lblTitle.Width; 
   int left = whiteSpace / 2; //half of whitespace is on left, half on right
   lblTitle.Location = new Point(left, lblTitle.Location.Y); //new value for X, Y stays the same
}