How to Auto Resize and Adjust Form Controls with Change in Resolution

How to auto resize and adjust Form controls with change in resolution

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.

Make forms fit to every size of screen

HI For responsive design
1st create below class

 public class Resolution
{
float heightRatio = new float();
float widthRatio = new float();
int standardHeight, standardWidth;
public void ResizeForm(Form objForm, int DesignerHeight, int DesignerWidth)
{
standardHeight = DesignerHeight;
standardWidth = DesignerWidth;
int presentHeight = Screen.PrimaryScreen.WorkingArea.Height;//.Bounds.Height;
int presentWidth = Screen.PrimaryScreen.Bounds.Width;
heightRatio = (float)((float)presentHeight / (float)standardHeight);
widthRatio = (float)((float)presentWidth / (float)standardWidth);
objForm.AutoScaleMode = AutoScaleMode.None;
objForm.Scale(new SizeF(widthRatio, heightRatio));
foreach (Control c in objForm.Controls)
{
if (c.HasChildren)
{
ResizeControlStore(c);
}
else
{
c.Font = new Font(c.Font.FontFamily, c.Font.Size * heightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
}
}
objForm.Font = new Font(objForm.Font.FontFamily, objForm.Font.Size * heightRatio, objForm.Font.Style, objForm.Font.Unit, ((byte)(0)));
}

private void ResizeControlStore(Control objCtl)
{
if (objCtl.HasChildren)
{
foreach (Control cChildren in objCtl.Controls)
{
if (cChildren.HasChildren)
{
ResizeControlStore(cChildren);

}
else
{
cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * heightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
}
}
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
else
{
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
}
}

then when ever yo add any form the add panel control to form and dock
it to form
below

InitializeComponent();

Write below code

    this.WindowState = FormWindowState.Maximized;
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
Resolution objFormResizer = new Resolution();
objFormResizer.ResizeForm(this, screenHeight, screenWidth);

this will make form responsive as much as possible as well as create system default font as well

Resize Controls with Form Resize

I found an alternative solution that is working well for me, appreciate any negative or positive comments on the solution.

Using several Split Containers and Split Containers inside of Split Containers in different regions I am able to section off the primary pieces of the layout, and within there utilizing Docking and Anchoring I am able to accomplish exactly what I wanted to do - it works beautifully.

I would point out I am aware that some folks online mention split containers use lots of resources.

How to automatically resize or reposition controls on a form when the form is resized?

It's possible to iterate through all of the controls on the form (mostly) programmatically, rather than having to explicitly adjust each control. You may have to put in some exceptions for some types of controls (such as timers, which I've put in the example), but generally you can use something like:

  Option Explicit

Private Type ControlInfo_type
Left As Single
Top As Single
Width As Single
Height As Single
FontSize As Single
End Type
Dim ControlInfos() As ControlInfo_type

Private Sub Form_Load()

Dim ThisControl As Control

ReDim Preserve ControlInfos(0 To 0)
ControlInfos(0).Width = Me.Width
ControlInfos(0).Height = Me.Height
For Each ThisControl In Me.Controls
ReDim Preserve ControlInfos(0 To UBound(ControlInfos) + 1)
On Error Resume Next ' hack to bypass controls with no size or position properties
With ControlInfos(UBound(ControlInfos))
.Left = ThisControl.Left
.Top = ThisControl.Top
.Width = ThisControl.Width
.Height = ThisControl.Height
.FontSize = ThisControl.FontSize
End With
On Error GoTo 0
Next

End Sub

Private Sub Form_Resize()

Dim ThisControl As Control, HorizRatio As Single, VertRatio As Single, Iter As Integer

If Me.WindowState = vbMinimized Then Exit Sub

HorizRatio = Me.Width / ControlInfos(0).Width
VertRatio = Me.Height / ControlInfos(0).Height

Iter = 0
For Each ThisControl In Me.Controls
Iter = Iter + 1
On Error Resume Next ' hack to bypass controls
With ThisControl
.Left = ControlInfos(Iter).Left * HorizRatio
.Top = ControlInfos(Iter).Top * VertRatio
.Width = ControlInfos(Iter).Width * HorizRatio
.Height = ControlInfos(Iter).Height * VertRatio
.FontSize = ControlInfos(Iter).FontSize * HorizRatio
End With
On Error GoTo 0
Next

End Sub

I tested this with the default form with a CommandButton, Frame, Timer, and TextBox, and it seemed to work OK. You'll probably want to tune the limits on the minimum and maximum sizes for appearance, and my handling of the font is very crude; this could be optimized also. But perhaps this could be a starting point.

This code depends upon the controls iterating the same way each time, which could conceivably break. One way around this would be to use a Collection or other data structure with the name of the control as a key; when iterating in the .Resize event, each control would be looked up by name. Additional structure will be necessary if any of the controls are themselves arrays, and even more if controls are loaded or unloaded dynamically.

How can I re-size controls based on resolution?

TL;DR: You can't without manually coding resizing logic. As a side note, you probably shouldn't be trying to do this.

In traditional WinForms there is no automatic way to my knowledge without rolling your own solution. Sean87 suggested the AutoScaleMode property, but while it is then automatic it doesn't support auto-sizing by resolution. It gives an option for scaling based on Font size settings in Windows or the DPI setting. Neither of these are directly changed when simply changing the screen resolution and, honestly, most typical users (and even probably power users) probably never modify the DPI or font size settings.

Besides, the whole point, from a basic user perspective, of increasing screen resolution is to give more virtual desktop "space" to place application windows in. True, most monitors are LCD-based now and thus have native resolutions that make changing this around mostly a thing of the CRT past. But still, the act of buying a new, larger monitor would let a typical user increase this virtual screen space so that they could see more windows on the screen simultaneously. An application that always maintains the same relative size to the screen resolution would be quite uncommon I think and goes against the expectations of most users, including even us advanced developer users.

Typically, you are to code your application to adjust its contents intelligently and usefully if the window is resized, but generally you defer to the user for managing that as it is their machine and their preference (think of it as the "my house, my rules" principle). This is also why most (all?) applications don't change window positions except when directly manipulated by the user (via dragging the window frame and such). In fact, all of this likely explains why Microsoft chose not to include any automatic sizing rule based on resolution.

Best way to resize form/controls according to resolution?

You can get the current screen dimensions with the following:

Dim screenHeight As Integer = My.Computer.Screen.Bounds.Height
Dim screenWidth As Integer = My.Computer.Screen.Bounds.Width

As for resizing, if you anchor your controls to the form you should be able to re-size the form to fit the dimensions of the screen as obtained above.

How to autosize the form window based on screen resolution during run time (Where the size of the window created during design is too large)

Setting the WindowState = Maximized will cause the window to open to the full extents of the screen resolution. It doesn't matter what that value is, it will match it.

Sample Image

Edit:
From your comments, it sounds like you want what the AutoSize property will accomplish. I updated the form to add some controls and set the AutoSize = True and the AutoSizeMode = GrowAndShrink. Between these three properties, you should be able to get the form to do exactly what you wish. The one thing to pay attention to is the full extents of your controls within the form. From this picture you can see the form during runtime will resize to fit both text boxes while in the editor, I shrunk the form to hide almost everything. Please also note that in the example below, I set the WindowState = Normal.

Sample Image



Related Topics



Leave a reply



Submit