Results 1 to 10 of 10

Thread: [RESOLVED] treeview expand a particular node by code. Possible?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Resolved [RESOLVED] treeview expand a particular node by code. Possible?

    Hello VbForums
    I can open the first node by this code.
    Code:
    TreeView1.Nodes(1).Expanded = True
    But I want to open the 4th node.
    Code:
    TreeView1.Nodes(4).Expanded = True
    this code doesn't open the 4 th node.
    thank you

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: treeview expand a particular node by code. Possible?

    Does Nodes(4) have any children?

    Sample using a TreeView control and a CommandButton
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      With TreeView1
        .Nodes.Clear
        .Nodes.Add , , "root", "Node 1"
        .Nodes.Add "root", tvwChild, "node1.1", "Node 1.1"
        .Nodes.Add "root", tvwChild, "node1.2", "Node 1.2"
        .Nodes.Add "root", tvwChild, "node1.3", "Node 1.3"
        .Nodes.Add "node1.3", tvwChild, "node1.3.1", "Node 1.3.1"
        .Nodes.Add "node1.3", tvwChild, "node1.3.2", "Node 1.3.2"
        .Nodes.Item(1).Expanded = True
      End With
    End Sub
    
    Private Sub Command1_Click()
      TreeView1.Nodes.Item("node1.3").Expanded = True
    End Sub
    Clicking the command button will open "Node 1.3.2", but only if the parents are also open.
    If you remove " .Nodes.Item(1).Expanded = True" line then clicking the command button doesn't do anything

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: treeview expand a particular node by code. Possible?

    Thank you sir for the interest and help
    Does Nodes(4) have any children?
    yes

    If you remove " .Nodes.Item(1).Expanded = True, then ....
    But I do not want to open node 1
    I'm feeding the treevew from database that why I could not replicate your code.
    thank you
    Last edited by Adebiyi24; Sep 10th, 2021 at 08:24 AM.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,904

    Re: treeview expand a particular node by code. Possible?

    Important is that a node can only be shown expanded if it's parent nodes are also expanded.
    You can't expand a node if it's parents is collapsed

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: treeview expand a particular node by code. Possible?

    I want to produce something like this:
    Attachment 182297
    Name:  10-09-2021 01-18-06.png
Views: 144
Size:  2.4 KB

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: treeview expand a particular node by code. Possible?

    Do you think this is not possible by code?

  7. #7
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    178

    Re: treeview expand a particular node by code. Possible?

    Quote Originally Posted by Adebiyi24 View Post
    Do you think this is not possible by code?
    Yes it is possible:
    Code:
    Option Explicit
    
    Public Sub ExpandNode(ByVal oNode As MSComctlLib.Node)
    
        If Not oNode.Parent Is Nothing Then
            'If the parent node is not expanded, then expand the parent node
            If Not oNode.Parent.Expanded Then
                ExpandNode oNode.Parent
            End If
        End If
        oNode.Expanded = True
    End Sub
    
    Private Sub Command1_Click()
        ExpandNode TreeView1.Nodes.Item("node1.3")
    End Sub
    Private Sub Form_Load()
      With TreeView1
        .Nodes.Clear
        .Nodes.Add , , "root", "Node 1"
        .Nodes.Add "root", tvwChild, "node1.1", "Node 1.1"
        .Nodes.Add "root", tvwChild, "node1.2", "Node 1.2"
        .Nodes.Add "root", tvwChild, "node1.3", "Node 1.3"
        .Nodes.Add "node1.3", tvwChild, "node1.3.1", "Node 1.3.1"
        .Nodes.Add "node1.3", tvwChild, "node1.3.2", "Node 1.3.2"
        'do not expand root node
    '    .Nodes.Item(1).Expanded = True
      End With
    End Sub

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: treeview expand a particular node by code. Possible?

    gilman
    Thank you very much
    I got it
    thank you
    Last edited by Adebiyi24; Sep 11th, 2021 at 05:38 AM.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2021
    Posts
    108

    Re: treeview expand a particular node by code. Possible?

    I got it
    thank you

  10. #10
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    178

    Re: treeview expand a particular node by code. Possible?

    There's a problem with the original code, if a node in the path is spanded but the root is colapsed, the procedure fails, and not show the expanded node, try
    This works fine.
    Code:
    Option Explicit
    
    Public Sub ExpandNode(ByVal oNode As MSComctlLib.Node)
    
        If Not oNode.Parent Is Nothing Then
            'don't stop if the parent is expanded
    '        If Not oNode.Parent.Expanded Then
            ExpandNode oNode.Parent
    '        End If
        End If
        oNode.Expanded = True
    End Sub
    
    Private Sub Command1_Click()
        ExpandNode TreeView1.Nodes("node1.3.2")
    End Sub
    Private Sub Form_Load()
      With TreeView1
        .Nodes.Clear
        .Nodes.Add , , "root", "Node 1"
        .Nodes.Add "root", tvwChild, "node1.1", "Node 1.1"
        .Nodes.Add "root", tvwChild, "node1.2", "Node 1.2"
        .Nodes.Add "root", tvwChild, "node1.3", "Node 1.3"
        .Nodes("node1.3").Expanded = True
        .Nodes.Add "node1.3", tvwChild, "node1.3.1", "Node 1.3.1"
        .Nodes.Add "node1.3", tvwChild, "node1.3.2", "Node 1.3.2"
       .Nodes.Add "node1.3.2", tvwChild, "node1.3.2.1", "Node 1.3.2.1"
        'do not expand root node
    '    .Nodes.Item(1).Expanded = True
      End With
    End Sub
    if you uncoment the comented if block in ExpandNode the button don't work

Posting Permissions

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



Click Here to Expand Forum to Full Width