Resize that Form!

Need to resize that Form? Can't afford one of the commercial resize controls?

Don't worry, this could be your lucky day. This handy code snippet demonstrates how to add resizing capabilities to your Form in an instant.

The code consists of two main parts, the first in the Form_Load event, the second in Form_Resize.

The Form_Load code runs the first time your Form is loaded. It cycles through all your controls, storing individual dimensions in the Tag property of each. It also stores the height and width of the entire Form in two variables.

Now we have all our original dimensions stored, safe and sound.

The Form_Resize code runs whenever the Form is resized. It cycles through each control and resizes appropriately, depending on the new dimensions. In addition to changing the position, width and height, it also alters Font Size.

Just copy and paste the below code behind any Form. It is well commented and works brilliantly!

Note: This code will not work with the few, rare controls that don’t have Top, Left, Width, Height or Tag properties. The most common example of this is the Line control.

Code

Private lngFormWidth As Long
Private lngFormHeight As Long

Private Sub Form_Load()
    
    Dim Ctl As Control
    
    'Store form dimensions in variables
     
    lngFormWidth = ScaleWidth
    lngFormHeight = ScaleHeight
        
    'Store initial control dimensions
    'in the Tag property - with error
    'handling for those controls which
    'do not have properties such as Top
    '(eg, Line control)
    
    On Error Resume Next
    
    For Each Ctl In Me
        Ctl.Tag = Ctl.Left & " " & Ctl.Top & " " & _
            Ctl.Width & " " & Ctl.Height & " "
            Ctl.Tag = Ctl.Tag & Ctl.FontSize & " "
    Next Ctl
    
    On Error GoTo 0

End Sub

Private Sub Form_Resize()

    Dim D(4) As Double
    Dim i As Long

    Dim TempPoz As Long
    Dim StartPoz As Long

    Dim Ctl As Control
    Dim TempVisible As Boolean

    Dim ScaleX As Double
    Dim ScaleY As Double

        'Calculate the scale
        
    ScaleX = ScaleWidth / lngFormWidth
    ScaleY = ScaleHeight / lngFormHeight

    On Error Resume Next

    'Cycle through each control
        
    For Each Ctl In Me
        TempVisible = Ctl.Visible
        Ctl.Visible = False
        StartPoz = 1
            
        'Read data from the Tag property
             
        For i = 0 To 4
            TempPoz = InStr(StartPoz, Ctl.Tag, " ", _
                vbTextCompare)
                     
            If TempPoz > 0 Then
                D(i) = Mid(Ctl.Tag, StartPoz, _
                    TempPoz - StartPoz)
                StartPoz = TempPoz + 1
            Else
                D(i) = 0
            End If
                      
            'Move the control based on data
            'in the Tag property and the form
            'scale...
            
            Ctl.Move D(0) * ScaleX, D(1) * ScaleY, _
                D(2) * ScaleX, D(3) * ScaleY
            Ctl.Width = D(2) * ScaleX
            Ctl.Height = D(3) * ScaleY
            
            'Change font size
            
            If ScaleX < ScaleY Then
                   Ctl.FontSize = D(4) * ScaleX
            Else
                   Ctl.FontSize = D(4) * ScaleY
            End If
            
        Next i
        
        Ctl.Visible = TempVisible
        
    Next Ctl
    
    On Error GoTo 0
    
End Sub


Discount Blinds | Hot Sauce | Wholesale Products | Mammoth Mountain Properties | Faux Woodblinds