表单和滚动条

我遇到了一个小问题,这个问题让我很头疼。 我一直在寻找很长一段时间,但我仍然没有find如何去做。

我所拥有的是一个将在表单上创buildcombobox的小脚本。

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个奇数combobox。 这显然会离开页面。 我想要做的是创build一个容器来持有这些combobox的forms,其中有一个垂直滚动条,使用户可以滚动它们。

我应该可以创build一个滚动条,但我该怎么做,所以滚动条滚动combobox,但留下它的标签和下面的button,他们在哪里。

我正在寻找一些帮助/指针在哪里去帮助实现这一目标。

提前致谢。

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

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

 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 

现在, 在你的循环内

更改您的代码,而不是将combobox添加到窗体,它将它们*添加到FRAME容器*

在Ozgrid MVP站点上有很多的帮助:在运行时,在运行中创build控件

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

HTH

菲利普

嗨,这是一个子程序。 希望这可以帮助你的概念:)

 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子,所以请随时随地调用它。
  • 我没有设置左边的属性,你可以轻松地在ctrl.height下面执行
  • 如果您认为合适,更改其他属性