In a form_resize event your form will resize. What will not resize and relocate automatically are your controls. They will remain at the locations originally specified in the design view.
So your problem is how do I write code to resize and relocate my controls?
Use ratios. Use ratios. Use ratios. Use ratios. Use ratios. Use algebra. Use algebra. Use Equations.
This is one example.
Find the new left position:
Ratio Formula
Original Width/ New Width = Original Left/ New Left
You know the original width of your form. Let's say it is 2200. After a resize event, it changes to 2800. And you know the current left position of your control:
Dim lngLeft as long
lngLeft = MyControl.Left
Use your formula:
2200 / 2800 = lngLeft / New Left
The only value you don't know is
New Left.
Cross Multiply to solve a ratio Equation: Multiply tops * bottoms. If you know algebra, then you should be able to figure this out.
But in the example it works like this:
2200 *
New Left = 2800 * lngLeft
To solve this equation isolate
NewLeft:
NewLeft = (2800 * lngLeft) / 2200
You have just solved for the left position of one control.
Use the same principle for solving for the top value, but use a different formula:
Original Height / New Height = lngTop / NewTop
In the resize event, you first extract the New Height. extract the current top value of your control. and then use the formula to figure out the
NewTop value.
Do the same for Width and Height values of the control.
Original Width / NewWidth = MyControl.Width / NewControlWidth
Isolate the only unknown value in the above equation (New Control Width) as above and set the width as before
Mycontrol.Width = NewControlWidth
Same for Height:
I'll let you figure that out equation.
But another problem that you'll face is how do I deal with 50 controls of different types?
I personally use the Controls Collection in VB6. You can stick this code inside your form in the appropriate places.
Option Explicit
Dim lngCurrentWidth as long, lngCurrentHeight as long
Private Sub Form_Load
lngCurrentWidth = Me.Width
lngCurrentHeight = Me.Height
End Sub
Private Sub Form_Resize()
On error Resume Next
Dim ctl as Control
dim strName as string
dim lngNewWidth as long, lngNewHeight as long
lngNewWidth = Me.Width
lngNewHeight = Me.Height
For each ctl in Me ' Me would refer to the form
' Find out which control with the TypeName function
strName = TypeName(ctl)
' You can deal with different controls in the same
' way or differently to suit your program using a
' Select Case Logic structure.
Select Case strName
Case "CommandButton", "Label", "TextBox"
' Code to deal with left, height, Width, and Height
lngLeft = ctl.left
lngTop = ctl.Top
lngWidth = ctl.Width
lngHeight = ctl.Height
' Solve your formulas here and assign the values to the control
ctl.left = WhateverVariable you've declared for this
ctl.Top = '[ ......
ctl.Width = ' ..................
' etc.
Case "PictureBox"
' Code to deal with left, height, Width, and Height
Case Else
End Select
Next
lngCurrentWidth = lngNewWidth
lngCurrentHeight = lngNewHeight
End Sub