Results 1 to 4 of 4

Thread: TreeView - Remember all expanded nodes

  1. #1

    Thread Starter
    Hyperactive Member squatman's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    337

    Question TreeView - Remember all expanded nodes

    Hi there,

    I have found examples and discussions on how to remember the last selected node, and reselect/expand that node after a refresh.

    What I want to do is remember all expanded nodes/node paths and after a refresh expand all previous paths.

    How would I go about this..? Before the refresh would I have to recursively check the state of each node, store it and then after the refresh reassign the restored state..?

    Any help or suggestion would be greatly appreciated.

    Thank you.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: TreeView - Remember all expanded nodes

    you could store all expanded nodes in a list, then after a refresh restore the expanded nodes:

    Code:
    Dim nodes As New List(Of TreeNode)
    
    Private Sub TreeView1_AfterCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCollapse
        nodes.Remove(e.Node)
    End Sub
    
    Private Sub TreeView1_AfterExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterExpand
        nodes.Add(e.Node)
    End Sub
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        nodes.Sort(Function(x, y) x.Level.CompareTo(y.Level))
        For Each n As TreeNode In nodes
            n.Expand()
        Next
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member squatman's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    337

    Re: TreeView - Remember all expanded nodes

    .paul.

    Thanks for the response, sorry for taking so long to return to this.

    I'm just looking at this again and can't seem to get it working.

    How does it know which TreeView it is expanding/collapsing after refresh..?

    Thank you.


    Edit:

    I have changed the code in the 'For' to:

    vb Code:
    1. TreeView1.Nodes(n.Name).Expand()

    To refer to the TreeView, I have tried removing the 'AfterCollapse' handler during the fresh so it doesn't mess with the list. But I still can't get this to work...

    Thanks again.


    Edit 2:

    I should probably note that some of the nodes will have the same 'name', this may be causing issues...

    Thanks
    Last edited by squatman; Oct 22nd, 2014 at 06:08 AM.

  4. #4

    Thread Starter
    Hyperactive Member squatman's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    337

    Re: TreeView - Remember all expanded nodes

    Ok, figured this one out.

    I had to Remove the handler for 'AfterExpand' whilst the 'For' was expanding the nodes.

    Thank you very much .paul.

    Another legend on these forums!


    Just going to have a play at; if a parent node is collapsed and never again expanded, after the refresh it doesn't expand the parent because the child nodes were still expanded.

    Any pointers would be appreciated.

    Thanks again.

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