Introduction
One of the problems that faces the programmers is the screen resolution and the sizes of the controls. Making the software changes screen resolution or obliges the user to do so are both annoying. Furthermore, the user may prefer sizes different of the ones chosen by the developer. Properties such as Anchor
and Docking
could be useful, but don't let the sizes of the controls preserve their initial ratios.
The Code
The following code allows the user to resize the form to any desired value while at the same time resizing the controls with the same ratio.
Option Strict On
Public Class Form1
Dim CW As Integer = Me.Width Dim CH As Integer = Me.Height Dim IW As Integer = Me.Width Dim IH As Integer = Me.Height
Private Sub Form1_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Resize
Dim RW As Double = (Me.Width - CW) / CW Dim RH As Double = (Me.Height - CH) / CH
For Each Ctrl As Control In Controls
Ctrl.Width += CInt(Ctrl.Width * RW)
Ctrl.Height += CInt(Ctrl.Height * RH)
Ctrl.Left += CInt(Ctrl.Left * RW)
Ctrl.Top += CInt(Ctrl.Top * RH)
Next
CW = Me.Width
CH = Me.Height
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
IW = Me.Width
IH = Me.Height
End Sub
End Class
Now, try to add some controls on the form, and then run the program. Try to resize the form to any value (you can even maximize it), and see how the controls are resized accordingly.
See the attached example.
Please note that the attached example is not a well designed application; it is just a simple example that clarifies the technique.