Results 1 to 4 of 4

Thread: Sort Treeview Nodes

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    6

    Sort Treeview Nodes

    I want to sort all nodes in my treeview.
    I know only the function sorted=true, but it only sort the first level of my treeview (not the children of the children....)

    Does anybody have an idea ?

    A lot of thanks for me helpers !

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Try this, after you sort the parent nodes, the child nodes that you declared should have a sort method to sort the nodes that are connected to that.

    Here is an example, please correct if I'm wrong, because I'm not at my workstation.

    Code:
    dim ParentNode as new TreeNode("Parent") //Root node
    
    //Sub nodes under Root node (Childs)
    
    dim ChildNode1 as new TreeNode("Child 1") 
    dim ChildNode2 as new TreeNode("Child 2") 
    dim ChildNode3 as new TreeNode("Child 3") 
    dim ChildNode4 as new TreeNode("Child 4") 
    
    Treeview1.nodes.add(ParentNode)
    ParentNode.Nodes.Add(ChildNode1)
    ParentNode.Nodes.Add(ChildNode2)
    ParentNode.Nodes.Add(ChildNode3)
    ParentNode.Nodes.Add(ChildNode4)
    
    Treeview1.nodes.sort()
    ParentNode.Nodes.sort()
    Try and see if that works

    Hope that helps
    Dont gain the world and lose your soul

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    6
    the problem is that the sorted property is aplied on the treeview class and node on the collection of nodes or on the node directly :

    treeview1.sorted=true;

    And I didn't found an other function or property !
    So I can't use your line ParentNode.Nodes.sort() or something like that.

  4. #4
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: Sort Treeview Nodes

    I believe .Net 2.0 has a nodesorter property, here's how I resolved the alpha sort problem in Net 1.0

    The code:

    sub forceTreeviewSort()

    '''''''''''''''''''''''''''''''''''''''''''''''
    ' Platform: Visual Basic 2002 NET 1.0 (VB7)
    '
    ' Purpose:
    ' Simple method to force sort after changes to treenode.text
    '
    ' Assumptions:
    ' node.text has been modified, node is no longer in alphabetical order
    ' Treeview on Form1 with a topnode already added
    ' Form1.Treeview name = "treeMain"
    ' Children and grandchildren may exist and need to be retained
    '
    '
    ' Solution:
    ' Copy the topnode to a temporary node.
    ' Clear the treeview.
    ' Add the temporary node to the treeview
    '
    ' Tested: Temporary node retains names clildren and grandchildren
    '
    '''''''''''''''''''''''''''''''''''''''''''''''
    ' Copy the existing topnode to a temporary variable
    Dim tvwTempNode As TreeNode = treeMain.TopNode


    'treemain.sorted = true 'if you haven't already done this

    ' Stuff that enhances processing
    treeMain.CollapseAll()
    treeMain.BeginUpdate()


    ' clear all the nodes on the treeview
    treeMain.Nodes.Clear()

    ' Debug
    MsgBox("Treemain cleared")
    ' Debug
    MsgBox("Procced with update?")

    ' add the temporary node back to the treeview
    treeMain.Nodes.Add(tvwTempNode)

    treeMain.ExpandAll()
    treeMain.EndUpdate()

    tvwTempNode = Nothing

    End Sub 'forceTreeviewSort

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