Returning checked Nodes from Treeview form control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the Treview form control on a userform.
I have a Root node called "Root" and a Child Node Called "Charts" than I
have child or grandchild nodes off of the "Charts" Node, 5 of them to be
exact which are named "c1", "c2", "c3", "c4" and "c5". all of the nodes in my
tree have empty check boxes beside the node name. What I would like to be
able to do is to go through and check for example nodes "c2" and "c4" ect..
and then programaticly have vba return in a message box whcih nodes were
checked.

Any Ideas on how to do this ?

Dan.
 
Tree walking routines... I hate them too.

Drop a TreeView control and a CommandButton control onto a UserForm then use
this example code.


Private Sub UserForm_Initialize()
Dim nod As Node

TreeView1.CheckBoxes = True

With TreeView1.Nodes
Set nod = .Add(Relationship:=tvwChild, Text:="A1")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="A2")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="A3")
Set nod = .Add(Relationship:=tvwChild, Text:="B1")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="B2")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="B3")
Set nod = .Add(Relationship:=tvwChild, Text:="C1")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="C2")
Set nod = .Add(Relative:=nod, Relationship:=tvwChild, Text:="C3")
End With
End Sub

Private Sub CommandButton1_Click()
Dim nod As Node, str As String

If TreeView1.Nodes.Count > 0 Then
Set nod = TreeView1.Nodes(1)

Do
If nod.Checked Then str = str & IIf(str = "", "", ", ") &
nod.Text
If Not nod.Child Is Nothing Then
Set nod = nod.Child
ElseIf Not nod.Next Is Nothing Then
Set nod = nod.Next
Else
Do
Set nod = nod.Parent
If Not nod Is Nothing Then
If Not nod.Next Is Nothing Then
Set nod = nod.Next
Exit Do
End If
Else
Exit Do
End If
Loop
If nod Is Nothing Then Exit Do
End If
Loop

MsgBox str

End If
End Sub
 
Back
Top