PDA

View Full Version : [SOLVED] Expand TreeView and select a node by parameter



Kaizer
01-27-2005, 07:43 AM
On a form I have a treeView and a cmbbox. Cmbbox contains the same values as the nodes.text of the treeView. I would like, by selecting a value from cmbbox, to expand a treeview and select the nodes.element=cmbbox.value.

Can you help with the code?

Jacob Hilderbrand
01-27-2005, 06:45 PM
This code demonstrates how to use the Combo Box to change the Tree View. The second Sub is only here to fill in the values for the Tree View and Combo Box.


Option Explicit

Private Sub ComboBox1_Click()
On Error Resume Next
Me.TreeView1.Nodes(Me.ComboBox1.ListIndex + 1).Parent.Parent.Expanded = True
Me.TreeView1.Nodes(Me.ComboBox1.ListIndex + 1).Parent.Expanded = True
Me.TreeView1.Nodes(Me.ComboBox1.ListIndex + 1).Expanded = True
On Error GoTo 0
End Sub

Private Sub UserForm_Initialize()
Dim i As Long
Dim j As Long
Dim k As Long
Dim Title As String
Dim xNode As Node
Dim NodeKey As String
Dim NodeKey2 As String
With Me.TreeView1
For i = 1 To 5
Set xNode = .Nodes.Add
NodeKey = "Node - " & i
With xNode
.Key = NodeKey
.Text = "Node - " & i
.Expanded = False
End With
Me.ComboBox1.AddItem ("Node - " & i)
For j = 1 To 7
Set xNode = .Nodes.Add(NodeKey, tvwChild)
NodeKey2 = "Node - Child - " & i & j
With xNode
.Key = NodeKey2
.Text = "Child - " & j
End With
Me.ComboBox1.AddItem ("Child - " & j)
For k = 1 To 10
Set xNode = .Nodes.Add(NodeKey2, tvwChild)
With xNode
.Text = "Child2 - " & k
End With
Me.ComboBox1.AddItem ("Child2 - " & k)
Next k
Next j
Next i
End With
Set xNode = Nothing
End Sub


Refer to the attachment.

Kaizer
01-28-2005, 05:16 AM
Thanks for the reply, Jake. I checked your code and realized that I need a little different. What I want to do is to:

1. Fill the cmbBox with the certain values of the treeView. I fill my cmbBox by RowSource=Structure!G2:G162. They are all sorted.
2. By selecting a value from cmbBox to Expand the treeView and select the same node.Text=cmbBox1.value

I have attached my file with the treeView and cmbBox.

Jacob Hilderbrand
01-28-2005, 05:52 AM
Try this code. I didn't see a way to actually select the node, but this will make the node visible in the TreeView window.



Private Sub cmbSearch_Change()
Dim i As Long
For i = 1 To Me.treeWorking.Nodes.Count
If Me.treeWorking.Nodes(i).Text = Me.cmbSearch.Text Then
Me.treeWorking.Nodes(i).EnsureVisible
Exit For
End If
Next i
End Sub

Kaizer
01-28-2005, 06:30 AM
Thank you Jake. That will work.

Kaizer

Jacob Hilderbrand
01-28-2005, 07:19 AM
You're Welcome :beerchug:

Take Care