PDA

Click to See Complete Forum and Search --> : Sort Treeview Nodes


frblondin
Feb 9th, 2002, 01:16 PM
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 !

DevGrp
Feb 9th, 2002, 02:48 PM
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.


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

frblondin
Feb 9th, 2002, 02:55 PM
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.

weaver999
May 7th, 2006, 03:34 PM
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