Home > OS >  VBA: How can UserForms with the elements contained be scaled proportionally to maintain the relation
VBA: How can UserForms with the elements contained be scaled proportionally to maintain the relation

Time:01-25

How can a UserForm with all contained elements be scaled proportionally?

I have a function to scale the UserForm itself. However, the inner elements (head, body) do not scale with it and are therefore shifted and the size no longer fits.

I have created the UserForm with the graphical tool. I'm looking for a way to group the elements like in PowerPoint (they are then all scaled proportionally when resized so that the relations are maintained).

Thanks a lot

I have tried to scale all elements individually, but this is very cumbersome, error prone and in my opinion bad style.

I tried to work with one frame.

CodePudding user response:

There is no build-in method to zoom a form. As already mentioned in the comments, you can play around by modifying the left, top (and width and height) properties of the controls. You should also change the font size of controls (if they have any).

The following gives you an idea how this could work:

Const ZoomStep = 1.1

Private Sub buttonLarger_Click()
    zoomForm ZoomStep
End Sub

Private Sub buttonSmaller_Click()
    zoomForm 1 / ZoomStep
End Sub

Sub zoomForm(factor As Double)
    Me.left = Me.left * factor
    Me.top = Me.top * factor
    Me.Width = Me.Width * factor
    Me.height = Me.height * factor
    
    Dim ctrl As Control
    For Each ctrl In Me.Controls
        ctrl.left = ctrl.left * factor
        ctrl.top = ctrl.top * factor
        ctrl.Width = ctrl.Width * factor
        ctrl.height = ctrl.height * factor
        On Error Resume Next  ' Prevent runtime error for controls w/o font
        ctrl.Font.Size = ctrl.Font.Size * factor
        On Error GoTo 0
    Next
End Sub

However, this has of course some limitations. Checkboxes and Radiobuttons for example don't size (only the caption). Buttons need some space around the caption and if you make the controls smaller, the caption may get unreadable.

  • Related