Reputation: 21
Is there a way to make everything automatically scale on a WinForm when maximizing the screen or changing resolution.
I found this to manual scale it correct but when switching resolution I have to change it every time.
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
Upvotes: 2
Views: 8459
Reputation: 244981
There's no simple switch you can press to make this happen automatically. Auto-scaling is for solving a very different problem. You need to lay out your form and its controls with this design goal in mind.
In particular, the best way to do it is to use a TableLayoutPanel
control docked to "fill" your entire form—set its Dock
property to DockStyle.Fill
. This will essentially become the "layout grid" that you use to lay out the child controls you wish to appear on the form.
Then, place each of your regular controls inside of the "cells" of that TableLayoutPanel
control. Set the Anchor
property of the child controls to indicate how you wish for them to grow when the form is expanded (or shrunk). For example:
This takes a bit of futzing to get right. For example, you will occasionally have to set the ColumnSpan
and/or RowSpan
properties of controls inside of the TableLayoutPanel
control to ensure that they arrange the way you want them to, particularly relative to other controls displayed on your form.
But it is just about the only way to do what you desire, and it does work very well once you get it set up.
Upvotes: 3