1

I have a label centered by the X coordinate on a form, and I'm trying to make it adjust its location on form resize. So, I'm handling the Resize event to update label Location property.

private void SecondaryWindow_Resize(object sender, EventArgs e)
{
    lblStartPointX = this.Width / 2 - lblSecondary1.Width / 2;
    lblStartPointY = lblSecondary1.Location.Y;
    lblSecondary1.Location = new Point(lblStartPointX, lblStartPointY);
}

It seems I can't directly assign some value to lblSecondary1.Location.X property (compiler considers it a mistake), so I came up with this code.

But it strikes me as not very efficient way to do the task... Just how much Point objects are created if you constantly resize the window, I wonder?

Bredorub
  • 13
  • 4
  • 3
    Possible duplicate of [How do I keep a label centered in WinForms?](https://stackoverflow.com/questions/4343730/how-do-i-keep-a-label-centered-in-winforms) – leopcode May 28 '19 at 06:16
  • _Just how much Point objects are created_ - did you observed performance issues? – Fabio May 28 '19 at 07:16
  • Does the absence of performance issues means my code is OK? Is it really a good idea to write the code without consideration for creation of excessive variables in the long run? – Bredorub May 28 '19 at 07:30

3 Answers3

1

There are multiply ways. I would recommend using WPF instead for responsive design.

When the Label is the only control, you can turn of Autosize, set the anchors to all sides, set the TextAlgin to Center and resize your Label, so it takes the whole space.

An other way would be to work with TableLayoutPanels. Takes some time to get used to it, but it comes near the Grid of WPF.

JP-Hundhausen
  • 70
  • 1
  • 7
  • Thanks for the answer, but what if I need to do it with code? – Bredorub May 28 '19 at 07:36
  • Why would you do that? You can do it your way. When you want to add a label via Code/Runtime, you still can set the anchor and so on. When you dont know how, steal it from the *.designer.cs file of your form. – JP-Hundhausen May 28 '19 at 12:13
1

Since you want your Label to remain centered in the X coordinates when the Form resizes, move it's position to the center of the Form when it loads, setting the the Label's Anchor to AnchorStyles.Top:

private void form1_Load(object sender, EventArgs e)
{
    label1.Anchor = AnchorStyles.Top;
    label1.Location = new Point((this.Width - label1.Width) / 2, label1.Top);
}

It will keep its position when the Form.Width is resized.

If you want to keep it centered in both dimension, center it and remove any anchors. The control will remain in the middle of its parent Form:

private void form1_Load(object sender, EventArgs e)
{
    label1.Anchor = AnchorStyles.None;
    label1.Location = new Point((this.Width - label1.Width) / 2, 
                                (this.Height - label1.Height) / 2);
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
0

you can use : 2 options 1) go to label properties in that goto Layout >> Anchor then set the anchor as you want it. 2) go to label properties in that goto Layout >> Dock then set the Dock as required.