2013-03-25 49 views
0

我遇到了一个轻微的问题,这导致我很头痛,试图解决。我一直在寻找很长时间,但我仍然没有找到如何去做。窗体和滚动条

我所拥有的是一个将在表单上创建组合框的小脚本。

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个奇数的组合框。这显然会消失。我想要做的是创建一个容器,以便在具有垂直滚动条的窗体中放置这些组合框,以便用户可以滚动浏览它们。

我应该能够创建一个滚动条,但我该怎么做,所以滚动条滚动组合框,但留下它的标签和它下面的按钮,他们在哪里。

我正在寻找一些帮助/指针去帮助实现这一点。

在此先感谢。

回答

2

您不能将组合框放在容器控件(如框架)中,然后添加滚动条(设置水平和垂直的方向属性)。

所以,你的循环之外,添加相框:

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 

现在,你的循环

变化里面你代码,以便将组合框添加到窗体中,而不是将其添加到框架容器*

有帮助为这个伟大的交易上Ozgrid MVP Site: Creating Controls at Runtime, On the Fly

让我们知道您的身体情况如何

HTH

菲利普

+0

是的,但我不知道该怎么做。我通过代码而不是图形设计来写它。如果我以图形的方式做了,我不会有问题,但是它在代码中执行。 – NoLiver92 2013-03-25 17:28:02

+0

@ NoLiver92整个表单在内存中构建?看起来像你应该能够使用表单GUI来创建这些最初,然后操纵控制/等。与VBA。如有疑问,请创建一个类似控制的虚拟表单并查看其属性。 – 2013-03-25 17:42:47

+0

我已经更新了我的代码*,并为您提供了一个链接,您可以从中获取一些示例代码和示例工作簿* – 2013-03-25 17:46:25

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 

观光修改:

  • 我用UserForm_Click()事件调用AddCombo子,所以请致电 它在任何你想要。
  • 我没有设置left属性,您可以轻松地做到这一点下面 ctrl.height线等
  • 更改属性,你认为合适