Treeview Reverse Sorting

you may have a treeview like this:

Main Node
  + The Parent
   | - A
   | - B
   | - C
   | - D


This routine will sort Z-A
so the result should be

Main Node
  + The Parent
   | - D
   | - C
   | - B
   | - A


Private Sub Command1_Click()
   Dim tvn As Node
   Dim tvn2 As Node
   
   sName = 1  ' Change Node 1
   ' or sName = "MyNodeName"
   
   Set tvn = TreeView1.Nodes(sName)
   tvn.Sorted = True   ' firstly sort A-Z  then we simply replace last items with the first ones
   tvn.Sorted = False
   
   chil = tvn.Children: If chil = 0 Then Exit Sub     ' if no children the exit
   Set tvn = tvn.Child.LastSibling
   
   For a = 1 To chil
       Set tvn2 = tvn
       
       Set tvn = tvn.Previous
       TreeView1.Nodes.Remove tvn2.Index
               
       TreeView1.Nodes.Add sName, 4, tvn2.Key, tvn2
   Next
End Sub

You might also like...

Comments

Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”