J'essaie de créer un User_form dynamique, où tous les Controls
sont créés au moment de l'exécution.Formulaire utilisateur créé dynamique, avec 2 zones de liste déroulante dépendantes
J'ai 2 tableaux de Combo-Boxes, le premier tableau de Combo-Boxes est "Catgeory" (CatCBArr
), et le deuxième tableau de Combo-Boxes est "Item" (ItemCBArr
).
Je veux, qu'une fois que je sélectionne une valeur de la première zone de liste déroulante de "Catégorie", disons CatCBArr(0)
, que seuls les éléments liés dans ItemCBArr(0)
seront affichés.
émission: je ne peux pas comprendre comment modifier la deuxième liste déroulante (ItemCBArr(0)
) en fonction de la valeur sélectionnée dans la première liste déroulante (CatCBArr(0)
)
User_Form code (section pertinente)
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
de CComboBox classe 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
Capture d'écran-GUI User_Form
'ComboBoxEvents.Value' renvoie la valeur. Vous pouvez faire un cas de sélection sur cette valeur et ensuite utiliser la deuxième méthode de liste déroulante '.addItem' pour ajouter les éléments dont vous avez besoin. Pour effacer les éléments dans le second coup droit, utilisez '.clear' –
Ah, je vois que vous déclarez également les comboboxes dans le userform avec' Dim'. Utilisez 'Public' à la place pour le second tableau (Dim se comporte comme privé) Dans ce cas, vous pouvez accéder aux comboboxes de votre classe via' User_form.ItemCBArr (0) ' –