3

我一直在尝试编写一些代码来在调整表单大小时处理调整大小和移动控件。

我的方法是为每个需要锚定的控件维度(即顶部/左侧/宽度/高度)创建一个字典对象,键是控件名称和锚维度(例如 lstAccounts_Height),并将这些添加到表单级别收藏。在表单Resize事件中,集合被迭代,每个控件都根据需要进行调整。

添加控件的例程:

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

并在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"

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

我面临的问题是在第一次移动事件中有一个小的“跳跃”,因此运行时控件与设计时的控件并不完全一致。我试图通过将表单的返回高度和宽度更改 4.5 来抵消这种影响,这确实有帮助。

sngFormMinimumHeightandsngFormMinimumWidth设置为事件中的起始宽度/高度或表单,Initialize我正在使用Chip Pearson 的代码使表单可调整大小。

我猜表格上有某种边界需要调整(因此 4.5s 有助于解决问题) - 谁能解释我需要调整哪些值?

解决方案感谢 BonCodigo 提供的链接,问题现已解决 - 我指的是Me.Height以及Me.Width何时应该指的是Me.InsideHeightand Me.InsideWidth。我现在不需要4.5的调整,“跳跃”现在没有了

4

1 回答 1

2

您可以尝试同时更改AnchorDock的属性,controls它们应该会自动调整其大小parent containers(可能是面板)。

于 2013-01-22T12:21:40.273 回答