Consulting

Results 1 to 6 of 6

Thread: Expand TreeView and select a node by parameter

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location

    Question Expand TreeView and select a node by parameter

    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?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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.

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    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.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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

  5. #5
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Thank you Jake. That will work.

    Kaizer

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    You're Welcome

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •