2013-03-25 50 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線等
  • 更改屬性,你認爲合適