我遇到了一个小问题,这让我头疼,试图解决。我一直在寻找很长一段时间,但我还没有找到如何做到这一点。
我所拥有的是一个可以在表单上创建组合框的小脚本。
For j = 0 To UBound(ComponentList) - 1
'Set Label
Set control = ComponentSelectionForm.Controls.Add("Forms.Label.1", "ComponentLabel" & CStr(j), True)
With control
.Caption = "Component " & CStr(j)
.Left = 30
.Top = Height
.Height = 20
.Width = 100
.Visible = True
End With
'set ComboBox
Set combo = ComponentSelectionForm.Controls.Add("Forms.ComboBox.1", "Component" & CStr(j), True)
With combo
.List = ComponentList()
.Text = "NONE"
.Left = 150
.Top = Height
.Height = 20
.Width = 50
.Visible = True
Set cButton = New clsButton
Set cButton.combobox = combo
coll.Add cButton
End With
Height = Height + 30
Next j
我发现有时候我可以拥有多达50个奇怪的组合框。这显然会离开页面。我想要做的是创建一个容器来容纳这些组合框在具有垂直滚动条的表单中,以便用户可以滚动它们。
我应该可以创建一个滚动条,但是我该如何操作以便滚动条滚动组合框但是将标签留在它上面以及它们下面的按钮。
我正在寻找一些帮助/指针来帮助实现这一目标。
提前致谢。
答案 0 :(得分:2)
无法将组合框放在容器控件(如Frame)中,然后添加滚动条(设置水平和垂直的方向属性)。
所以,在你的圈子之外,添加你的框架:
ComponentSelectionForm.Controls.Add("Forms.Frame.1", "fraContainer" , True)
然后将滚动条添加到容器
ComponentSelectionForm.Controls("fraContainer").controls.add("Forms.Scrollbar.1", "scHorizontal" , True)
with ComponentSelectionForm.Controls("fraContainer").controls("scHorizontal")
' set orentation to Horizontal
.orientation=1
end with
ComponentSelectionForm.Controls("fraContainer").controls.add("Forms.Scrollbar.1", "scVertical" , True)
with ComponentSelectionForm.Controls("fraContainer").controls("scVertical")
' set orentation to Vertical
.orientation=0
end with
现在,在你的圈内
更改您的代码,以便不将组合框添加到表单中,而是将它们*添加到FRAME容器中*
Ozgrid MVP Site: Creating Controls at Runtime, On the Fly
为此提供了大量帮助让我们知道你是如何进行的
HTH
菲利普
答案 1 :(得分:1)
嗨,这是一个子程序。希望这可以帮助你理解这个概念:)
Private Sub UserForm_Click()
Call AddCombo(30)
End Sub
Private Sub AddCombo(num As Integer, Optional gap As Single = 2.25, _
Optional ctrlHeight As Single = 18)
Dim ctrl As Control, i As Integer
Static lastTop As Single
lastTop = 2
For i = 1 To num
Set ctrl = UserForm1.Controls.Add("Forms.ComboBox.1", "Combo" & i, True)
With ctrl
If i = 1 Then
ctrl.Top = lastTop
ctrl.Height = ctrlHeight
'Add other codes here .....
Else
lastTop = lastTop + ctrlHeight + gap
ctrl.Top = lastTop
ctrl.Height = ctrlHeight
'Add other codes here .....
End If
End With
Next i
If lastTop > Me.Height Then
Me.ScrollHeight = lastTop
Me.ScrollBars = fmScrollBarsVertical
End If
End Sub
要修改的内容: