UserForm resizing code doesn't quite work

I am trying to write some code to handle resizing and moving controls when a form is resized.

My approach is to create a Dictionary object for each control that needs to be anchored (i.e. top / left / width / height), with the key being the name of the control and the size of the anchor (e.g. lstAccounts_Height) and add them to level assembly. The form event Resize

is reassembled and each control is customized as needed.

Procedure for adding 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 controls to the event Initialize

:

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 event Resize

:

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 having is that there is a slight "jump" in the first move event, and hence the runtime controls are not quite aligned as they run during design time. I tried to counter this effect by changing the return height and width of the form to 4.5, which helps.

sngFormMinimumHeight

and sngFormMinimumWidth

are set as the initial width / height or shape in the event Initialize

and I am using Chip Pearson code to change the shape.

I'm guessing there are some borders on the form that need to be adjusted (hence 4.5s helping this issue) - can someone please explain what values ​​I need to adjust with?

Permission . Thanks to the link provided by BonCodigo the problem is now resolved - I meant Me.Height

and Me.Width

when I had to link to Me.InsideHeight

and Me.InsideWidth

, Now I don't need the 4.5 setting and the "jump" is now gone

+3


source to share


1 answer


You can try changing the properties Anchor

and Dock

controls

, and they should be automatically resized with parent containers

(perhaps a panel).



+2


source







All Articles