Somewhere in all my clicking about I found this solution to a slightly different problem, but I think it can be easily adapted. To resize all the elements of a form when the form is resized do this:
'At the beginning of the form:
Private isLoading As Boolean = True
Private origFormSize As Rectangle
Private originalControlRectangles(100) As Rectangle
Private originalControlFontSizes(100) As Single
Private FontScale As Single = 1.0
'In the form.load event:
origFormSize = New Rectangle(Me.Location, Me.Size)
Dim controlCount As Integer
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
originalControlRectangles(controlCount) = New Rectangle(ctrl.Location, ctrl.Size)
originalControlFontSizes(controlCount) = ctrl.Font.Size
controlCount += 1
ctrl = Me.GetNextControl(ctrl, True)
Loop
isLoading = False
'In the form.resize event:
If isLoading Then Exit Sub
Dim controlCount As Integer
Dim ctrl As Control = Me.GetNextControl(Me, True)
Do Until ctrl Is Nothing
ResizeChildControl(ctrl, originalControlRectangles(controlCount), originalControlFontSizes(controlCount))
controlCount += 1
ctrl = Me.GetNextControl(ctrl, True)
Loop
'Add the method:
Private Sub ResizeChildControl(ByRef aControl As Control, ByRef origControl As Rectangle, ByRef origFontSize As Single)
Dim xRatio As Single, yRatio As Single
Try
xRatio = Me.ClientRectangle.Width / origFormSize.Width
yRatio = Me.ClientRectangle.Height / origFormSize.Height
Dim newX As Single = origControl.Location.X * xRatio
Dim newy As Single = origControl.Location.Y * yRatio
aControl.Location = New Point(CInt(newX), CInt(newy))
aControl.Width = CInt(xRatio * origControl.Width)
aControl.Height = CInt(yRatio * origControl.Height)
Dim ratio As Single
If xRatio >= yRatio Then
ratio = yRatio
Else
ratio = xRatio
End If
Dim newFontSize As Single
newFontSize = origFontSize * ratio * FontScale
Dim newFont As Font
newFont = New Font(aControl.Font.FontFamily, newFontSize)
aControl.Font = newFont
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I'll say again: I did not write this code, but I have tested it. It works well for smaller Windows forms, but not forms with more than, say, 25 controls.
Thank you to whoever did write this code it has saved me hours of form design time.