Home » excel » vba – UserForm resize code not quite working

vba – UserForm resize code not quite working

Posted by: admin April 23, 2020 Leave a comment


I have been trying to write some code to handle resizing & moving controls when the form is resized.

My approach is to create a Dictionary object for each Control dimension that needs to be anchored (i.e. top/left/width/height) with the key being the control name and anchor dimension (e.g. lstAccounts_Height) and to add these to a form-level Collection. On the form Resize event the Collection is iterated through, and each Control adjusted as necessary.

The routine to add controls:

Private Sub AddFormResizeControl(ControlName As String, _
                                 Dimension As String)

Dim strKey                      As String
Dim sngValue                    As Single
Dim dictCtrl                    As Dictionary

    strKey = ControlName & "_" & Dimension

    Select Case Dimension
        Case "Left": sngValue = Controls(ControlName).Left
        Case "Top": sngValue = Controls(ControlName).Top
        Case "Width": sngValue = Controls(ControlName).Width
        Case "Height": sngValue = Controls(ControlName).Height
    End Select

    Set dictCtrl = New Dictionary
    dictCtrl.Add strKey, sngValue

    If colResizeControls Is Nothing Then _
        Set colResizeControls = New Collection
    colResizeControls.Add dictCtrl, strKey

End Sub

And adding the controls in the Initialize event:

AddFormResizeControl "lst_AccountSelection", "Width"
AddFormResizeControl "lst_AccountSelection", "Height"
AddFormResizeControl "cmd_Cancel", "Left"
AddFormResizeControl "cmd_Cancel", "Top"
AddFormResizeControl "cmd_Confirm", "Left"
AddFormResizeControl "cmd_Confirm", "Top"

And the Resize event:

Private Sub UserForm_Resize()

Dim sngHeightAdjust             As Single
Dim sngWidthAdjust              As Single

Dim dict                        As Dictionary
Dim strCtrl                     As String

Dim ctrl                        As Control
Dim strDimension                As String

    If Me.Width < sngFormMinimumWidth Then Me.Width = sngFormMinimumWidth
    If Me.Height < sngFormMinimumHeight Then Me.Height = sngFormMinimumHeight

    sngHeightAdjust = (Me.Height - 4.5) - sngFormMinimumHeight
    sngWidthAdjust = (Me.Width - 4.5) - sngFormMinimumWidth

    If Not colResizeControls Is Nothing Then
        For Each dict In colResizeControls
            strCtrl = dict.Keys(0)
            Set ctrl = Controls(Left(strCtrl, InStrRev(strCtrl, "_") - 1))
            If Right(strCtrl, 5) = "_Left" Then
                ctrl.Left = dict.Item(strCtrl) + sngWidthAdjust
            ElseIf Right(strCtrl, 4) = "_Top" Then
                ctrl.Top = dict.Item(strCtrl) + sngHeightAdjust
            ElseIf Right(strCtrl, 6) = "_Width" Then
                ctrl.Width = dict.Item(strCtrl) + sngWidthAdjust
            ElseIf Right(strCtrl, 7) = "_Height" Then
                ctrl.Height = dict.Item(strCtrl) + sngHeightAdjust
            End If
        Next dict
    End If

End Sub

The problem I am facing is that there is a small “jump” on the first move event, and consequently the runtime controls are not quite aligned as they at design-time. I have tried to counter this effect by changing the returned height and width for the form by 4.5, which does help.

The sngFormMinimumHeight and sngFormMinimumWidth are set as the starting width/height or the form in the Initialize event, and I am using Chip Pearson’s code to make the form resizeable.

I’m guessing that there are some kind of borders on the form which need to be adjusted for (hence the 4.5s helping the issue) – can anyone explain what values I need to adjust by?

Resolution Thanks to the link provided by BonCodigo, the issue is now resolved – I was referring to Me.Height and Me.Width when I should have been referring to Me.InsideHeight and Me.InsideWidth. I now do not need the adjustment of 4.5 and the “jump” is now gone

How to&Answers:

You may try to change both Anchor and Dock properties of the controls and they should automatically resize with their parent containers (perhaps a panel).