I have a userform in which I have a TreeView and a ComboBox:
Private Sub UserForm_Initialize()
With TreeView1.Nodes
.Add , , "A", "Item1"
.Add "A", tvwChild, , "SubItem1"
.Add , , "B", "Item2"
.Add "B", tvwChild, , "SubItem2"
End With
With ComboBox1
.AddItem "Case1"
.AddItem "Case2"
.AddItem "Case3"
.AddItem "Case4"
End With
End Sub
My goal now is that when I select a parent node in the TreeView, that only certain items from the ComboBox are displayed to me. For example: I select Item2 and in the ComboBox Case 1 and Case 3 are only shown to me. What I tried was to make a If loop but it didn't work out.
How can I actually do this and is it possible?
CodePudding user response:
The TreeView1_NodeClick
event returns the Node
that was clicked. Use the Node.Text
value to determine which items are added to the ComboBox
.
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
With ComboBox1
.Clear
Select Case Node.Text
Case "A"
.AddItem "Case1"
.AddItem "Case3"
Case "B"
.AddItem "Case2"
.AddItem "Case4"
End Select
End With
End Sub
Addendum
OP pointed out that he needed to use the Node.Key
property for his use case.
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
With ComboBox1
.Clear
Select Case Node.Key
Case "A"
.AddItem "Case1"
.AddItem "Case3"
Case "B"
.AddItem "Case2"
.AddItem "Case4"
End Select
End With
End Sub