Ich versuche ein dynamisches Benutzerformular zu erstellen, in dem alle Controls
zur Laufzeit erstellt werden.Dynamisch erstelltes Benutzerformular mit 2 abhängigen Kombinationsfeldern
Ich habe 2 Array von Combo-Boxen, erste Reihe von Combo-Boxen ist "Catgeory" (CatCBArr
), und die zweite Reihe von Combo-Boxen ist "Artikel" (ItemCBArr
).
Ich mag, dass ich einmal einen Wert aus der ersten Combo-Box von „Kategorie“ wählen, lassen Sie sich CatCBArr(0)
sagt, dass nur der entsprechende Artikel in ItemCBArr(0)
angezeigt.
Ausgabe: Ich kann nicht herausfinden, wie die zweite Combo-Box zu modifizieren (ItemCBArr(0)
) entsprechend den ausgewählten Wert in der ersten Combo-Box (CatCBArr(0)
)
User_Form-Code (entsprechender Abschnitt)
Option Explicit
Dim ItemsNumofRows As Long
Dim QtyTB As MSForms.TextBox
Dim CatCB As MSForms.ComboBox
Dim ItemCB As MSForms.ComboBox
Dim Key As Variant
' dynamic Form controls (related to new Classes)
Dim CatCBArr() As New cComboBox
Dim ItemCBArr() As New cComboBox
Dim QtyTBArr() As New cTextBox
Private Sub UserForm_Initialize()
' reset flags
ItemsNumofRows = 5
TasksNamesUpd = False
TasksColUpd = False
ItemsRows_ControlsInit '<-- upload all Controls at run-time
Check_FormHeight
End Sub
'======================================================
Private Sub ItemsRows_ControlsInit()
For ItemRow = 0 To ItemsNumofRows
' add Category Combo-boxes
Set CatCB = Me.Controls.Add("Forms.ComboBox.1", "Cb" & ItemRow, True)
With CatCB
' loop through Dictionay items (view category)
For Each Key In Dict.Keys
.AddItem Key
Next Key
.SpecialEffect = fmSpecialEffectSunken
.Left = 40
.Width = 100
.Height = 18
.Top = 54 + 20 * ItemRow
ReDim Preserve CatCBArr(0 To ItemRow)
Set CatCBArr(ItemRow).ComboBoxEvents = CatCB
End With
' add Item Combo-boxes
Set ItemCB = Me.Controls.Add("Forms.ComboBox.1", "Cb_" & ItemRow, True)
With ItemCB
.SpecialEffect = fmSpecialEffectSunken
.Left = 160
.Width = 100
.Height = 18
.Top = 54 + 20 * ItemRow
ReDim Preserve ItemCBArr(0 To ItemRow)
Set ItemCBArr(ItemRow).ComboBoxEvents = ItemCB
End With
Next ItemRow
End Sub
CComboBox Class Code
Public WithEvents ComboBoxEvents As MSForms.ComboBox
Private Sub ComboBoxEvents_Change()
Dim CBIndex As Long
' get for ID number (row number), from third character in String Name.
' e.g "Cb1" will result 1)
CBIndex = CInt(Mid(ComboBoxEvents.Name, 3))
' ??? How do I get the Value, and update the second combo-box Items
Select Case ComboBoxEvents.Value
End Select
End Sub
GUI User_Form-screen shot
'ComboBoxEvents.Value' gibt den Wert zurück. Sie können einen Auswahlfall für diesen Wert eingeben und dann die zweite Combobox '.addItem'-Methode verwenden, um die benötigten Elemente hinzuzufügen. Um die Elemente in der zweiten Vorhand zu löschen, verwenden Sie '.clear' –
Ah ich sehe, dass Sie auch die Comboboxen im Benutzerformular mit' Dim' deklarieren. Verwenden Sie 'Public' stattdessen für das zweite Array (Dim verhält sich genauso wie private) In diesem Fall können Sie auf die Comboboxen von Ihrer Klasse über' User_form.ItemCBArr (0) ' –