Centering Controls Within a Form in .Net (Winforms)

How to set controls on center of the winform?

Place the control on the center and set Anchor property to None.

Centering controls within a form in winforms

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;
}

C# Winforms trouble centering a form over a control, can center it over other controls

Unfortunately, the time-consuming task itself is updating the UI and must exist on the UI thread for other reasons, but I was able to put together a solution by using a recursive class that got me the appropriate (x,y) coordinates to display a form in the middle of the control.

private static void RunWaiting(Control c, string text)
{
wf = new WaitingForm();
wf.drwbieSpinnerFrame.Text = text;
wf.ShowInTaskbar = false;
int[] tl = GetTopLefts(c);
wf.Top = (tl[0] + (c.Height / 2)) - (wf.Height / 2);
wf.Left = (tl[1] + (c.Width / 2)) - (wf.Width / 2);
wf.FormBorderStyle = FormBorderStyle.None;
wf.ControlBox = false;
wf.TopMost = true;
wf.StartPosition = FormStartPosition.Manual;
IsHolding = true;
Application.Run(wf);

}

And the method it calls to get the position data:

private static int[] GetTopLefts(Control c)
{
int top, left;
top = c.Top;
left = c.Left;
if (c.Parent != null)
{
int[] parentPoint = GetTopLefts(c.Parent);
top += parentPoint[0];
left += parentPoint[1];
}
return new int[] { top, left };
}

You could probably get through this just fine by making the final output of GetTopLefts() a Point, but something about this way felt arbitrarily more reliable.

How do I center content in a Panel?

Design Time Approach

At design time, put your button in your container and select your button. Then, use Center Horizontally and Center Vertically from Layout toolbar to put your button in center of panel. After, go to your buttons properties and remove every anchor from Anchor property.

Coding Approach

Panel panel = new Panel();
panel.Size = new Size(300, 100);
panel.Dock = DockStyle.Bottom;

Button button = new Button();
button.Size = new Size(70, 25);
button.Location = new Point((panel.Width - button.Width) / 2, (panel.Height - button.Height) / 2);
button.Anchor = AnchorStyles.None;

panel.Controls.Add(button);
this.Controls.Add(panel);

Keep a Control vertically and horizontally at center of its container

Sample Image

We can achive this by simple steps

  • Set Label Anchor to Left and Right
  • Set Label AutoSize to false ;
  • Set Label TextAlign to MiddleCenter;

now Place label middle of panel.

   int x = (panel1.Size.Width - label1.Size.Width) / 2;
label1.Location = new Point(x, label1.Location.Y);

Center Controls when form is Maximized

At design time you need to center your controls and then go to the anchoring property of each control and remove any anchoring(Left,Right,Top,Bottom) they have. This will ensure that when form is resized they remain at their design time position.

How to place a label or a button exactly in the middle of the Form?

Design time :

In my VisualStudio2010 I have these 2 buttons to center horizontally and vertically:

Sample Image

Its located in the toolbar "Layout". If it isn't, you can add them by clicking the small button to the right. It is also in the Format menu.

To keep centered at Runtime: Turn off all anchoring.

Note:This will keep the control at its relative position as long as it doesn't change it Size. If it does, like autosize Labels are prone to, you will have to code the Resize event. Examples are here



Related Topics



Leave a reply



Submit