sort treeview

  • Thread starter Thread starter Sam
  • Start date Start date
S

Sam

Hi,
How can I sort the root nodes of a treeview according to a integer
stored in their tag property? I want to do that at run time on a
button's click event.

Thank you
 
AFAIK, the Sorted property of the Treeview only sorts alphabetically, so you
will have to set it to false and sort the treenodes in an array (Array.Sort)
before adding them to the treeview, providing an IComparer that uses the Tag
property of the treenodes.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
I've used the following to do it: ;-)

Public Class NodesList
Inherits List(Of TreeNode)

Public Overloads Sub Sort()
MyBase.Sort(New NodeTagComparer())
End Sub
End Class

Public Class NodeTagComparer
Implements IComparer(Of TreeNode)

Public Function Compare(ByVal node1 As TreeNode, ByVal node2 As
TreeNode) As Integer Implements IComparer(Of TreeNode).Compare
Return (DirectCast(node1.Tag,
Query).SortOrder.CompareTo(DirectCast(node2.Tag, Query).SortOrder))
End Function
End Class

Dim lstNodes As New AdminSystemLib.NodesList
For Each n As TreeNode In Me.Nodes
lstNodes.Add(n)
Next
lstNodes.Sort()
 
Back
Top