Excel VBA树视图链接到组合框



我有一个用户表单,其中有一个TreeView和一个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

我现在的目标是,当我在TreeView中选择父节点时,只有组合框中的某些项目才会显示给我。例如:我选择了Item2,在组合框中,案例1和案例3只显示给我看。我试图进行If循环,但没有成功。

我怎么才能真正做到这一点,这可能吗?

TreeView1_NodeClick事件返回单击的Node。使用Node.Text值来确定将哪些项目添加到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

附录

OP指出,他需要将Node.Key属性用于他的用例。

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

相关内容

  • 没有找到相关文章

最新更新