-1

I have a form with several controls. The issue is that when I maximize the form to assume the screen resolution the controls do not scale correctly.

My Code:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control control in this.Controls)
    {             
        control.Anchor = 
        ((System.Windows.Forms.AnchorStyles) 
        ((((System.Windows.Forms.AnchorStyles.Top | 
        System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
    }
    this.Location = new Point(0, 0);
    this.Size = Screen.PrimaryScreen.WorkingArea.Size;
}

I have researched the problem for a few hours now and I have found the adjusting the anchor styles to Top, Bottom, Left, Right so that the controls will be resized along with the form. I have tried that with the code above and it has not helped. Setting DockSytle to DockStyle.Fill does not help either. If anyone could offer a suggestion of what would help me it would be greatly appreciated.

AussieJoe
  • 1,285
  • 1
  • 14
  • 29
  • Since this is clearly a winforms question, you should tag it as winforms so the people who focus on answering winforms questions will find it. – 15ee8f99-57ff-4f92-890c-b56153 May 16 '18 at 18:32
  • I tend to use a [TableLayoutPanel](https://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel(v=vs.110).aspx) when putting controls on forms. Then you have several options for scaling the controls inside it. – Deolus May 16 '18 at 18:42
  • What do you mean by "it has not helped"? – Rufus L May 16 '18 at 18:49
  • Also, are your controls inside another container control? If so, they will not be part of `this.Controls` – Rufus L May 16 '18 at 18:51
  • Possible duplicate of [How to write WinForms code that auto-scales to system font and dpi settings?](https://stackoverflow.com/questions/22735174/how-to-write-winforms-code-that-auto-scales-to-system-font-and-dpi-settings) – 41686d6564 stands w. Palestine May 16 '18 at 21:05

2 Answers2

1

The anchor style works like this. On a control if you set anchor points to:

Top/Right then the control stays in the Top Right. Top/Left then the control stays in the Top Left. Bottom/Right then the control stays in bottom right. Bottom/Left then the control stays in bottom left. Top then it stays at the top. Bottom then it stays in the bottom. Left then it stays in the Left. Right then it stays in the right. Top/bottom stretches top to bottom. Left/Right stretches left to right.

Now when you anchor a control to any of those combinations they will stay in that location when form is maximized. Controls can anchor to each other as well.

I hope this helps.

Halonic
  • 405
  • 2
  • 12
0

You have to use each of the anchor styles correctly.

Ideally, you should show your form and tell us the desired results.

But try this:

  • If you use "only one" style, the item will move (without resizing) attached to the side you chose
    • You can combine one vertical and one horizontal this way
  • If you use "two" opposite styles, the item will stretch when you stretch the form

If you have too many items, you may want to consider using the suggested TableLayoutPanel in the comments to your question.

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214