I have noticed that some applications change their controls position to adjust them as much as possible in the resolution as possible, If window is maximized they set themselves in such a way that over all GUI looks balanced. My question is that is it possible to make or implement this functionality in Visual studio 2010 C#?
10 Answers
Use Dock and Anchor properties. Here is a good article. Note that these will handle changes when maximizing/minimizing. That is a little different that if the screen resolution changes, but it will be along the same idea.
- 24,442
- 9
- 69
- 76
- 47,539
- 21
- 146
- 180
-
I must mention the AutoScaleMode which made me waist **lots** of time of games with Anchor&Dock (which weren't actually the problem...) – ephraim May 07 '18 at 11:28
Use combinations of these to get the desired result:
Set
Anchorproperty to None, the controls will not be resized, they only shift their position.Set
Anchorproperty to Top+Bottom+Left+Right, the controls will be resized but they don't change their position.Set the
Minimum Sizeof the form to a proper value.Set
Dockproperty.Use
Form Resizeevent to change whatever you want
I don't know how font size (label, textbox, combobox, etc.) will be affected in (1) - (4), but it can be controlled in (5).
- 1,028
- 12
- 16
float widthRatio = Screen.PrimaryScreen.Bounds.Width / 1280;
float heightRatio = Screen.PrimaryScreen.Bounds.Height / 800f;
SizeF scale = new SizeF(widthRatio, heightRatio);
this.Scale(scale);
foreach (Control control in this.Controls)
{
control.Font = new Font("Verdana", control.Font.SizeInPoints * heightRatio * widthRatio);
}
- 155
- 1
- 2
-
@AfnanBashir perhaps, but this one saves you tons of time, if you already have a Windows form with tons of controls. +1 – Oct 12 '15 at 19:39
-
That's an interesting approach (even if the ratio calculation could be improved). Ostensibly it relies on `AutoScaleMode` of the form being `Font`. – Zeus Jul 08 '21 at 02:31
..and to detect a change in resolution to handle it (once you're using Docking and Anchoring like SwDevMan81 suggested) use the SystemEvents.DisplaySettingsChanged event in Microsoft.Win32.
- 306
- 1
- 6
Here I like to use https://www.netresize.net/index.php?c=3a&id=11#buyopt. But it is paid version.
You also can get their source codes if you buy 1 Site License (Unlimited Developers).
How ever I am finding the nuget package solution.
- 2,732
- 1
- 18
- 27
sorry I saw the question late, Here is an easy programmatically solution that works well on me,
Create those global variables:
float firstWidth;
float firstHeight;
after on load, fill those variables;
firstWidth = this.Size.Width;
firstHeight = this.Size.Height;
then select your form and put these code to your form's SizeChange event;
private void AnaMenu_SizeChanged(object sender, EventArgs e)
{
float size1 = this.Size.Width / firstWidth;
float size2 = this.Size.Height / firstHeight;
SizeF scale = new SizeF(size1, size2);
firstWidth = this.Size.Width;
firstHeight = this.Size.Height;
foreach (Control control in this.Controls)
{
control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));
control.Scale(scale);
}
}
I hope this helps, it works perfect on my projects.
-
I'm getting the error `Value of '∞' is not valid for 'emSize'. 'emSize' should be greater than 0 and less than or equal to System.Single.MaxValue. Parameter name: emSize` in the line `control.Font = new Font(control.Font.FontFamily, control.Font.Size* ((size1+ size2)/2));` after using your code.. – Bumba Nov 29 '21 at 13:29
add this code at page load do for all control or add all control in containers
int x;
Point pt = new Point();
x = Screen.PrimaryScreen.WorkingArea.Width - 1024;
x = x / 2;
pt.Y = groupBox1.Location.Y + 50;
pt.X = groupBox1.Location.X + x;
groupBox1.Location = pt;
in the form load event add this line
this.WindowState = FormWindowState.Maximized;
- 389,931
- 88
- 552
- 692
- 1,614
- 1
- 17
- 28
private void MainForm_Load( object sender, EventArgs e )
{
this.Size = Screen.PrimaryScreen.WorkingArea.Size
}
- 579
- 6
- 10
this.WindowState = FormWindowState.Maximized;
- 6,154
- 10
- 49
- 50
-
4Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – ryanyuyu Sep 29 '16 at 13:15
-
3This code will only make your windows maximized, but the size of all controls inside the windows won't dynamically changed. – Vincent Elbert Budiman May 04 '18 at 05:37