我一直在尝试编写一些代码来处理调整大小和调整窗体大小时移动控件。
我的方法是为需要锚定的每个Control维度(即顶部/左侧/宽度/高度)创建一个Dictionary对象,其中键是控件名称和锚点维度(例如lstAccounts_Height)并将这些添加到a表单级集合。在表单Resize
上,迭代了Collection,并根据需要调整每个Control。
添加控件的例程:
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来对抗此效果,这有帮助。
sngFormMinimumHeight
和sngFormMinimumWidth
被设置为Initialize
事件中的起始宽度/高度或表单,我使用Chip Pearson's code使表单可以调整大小。
我猜测表格上有某种边界需要调整(因此4.5s有助于解决问题) - 有人可以解释我需要调整哪些值吗?
解决方案感谢BonCodigo提供的链接,问题现已解决 - 我指的是Me.Height
和Me.Width
,当时我应该指{{1} }和Me.InsideHeight
。我现在不需要调整4.5并且“跳跃”现在已经消失了
答案 0 :(得分:2)
您可以尝试更改Anchor
的{{1}}和Dock
属性,并且应该使用controls
(可能是面板)自动调整大小。