Solved

Sorting TreeView Nodes.

Posted on 2007-03-19
3
3,480 Views
Last Modified: 2007-12-19
I want to sort my TreeView alphabetically.

So all the Nodes, and all of their children are sorted alphabetically.

I have searched previous solutions and found this bubble search:


   public static void SortNodes(System.Windows.Forms.TreeNode _Node)
    {
      SortTreeNodes(_Node.Nodes);
    }

    public static void RecurseTree(System.Windows.Forms.TreeNodeCollection _Nodes, ProcessNodeDelegate FunctionToCall)
    {
      if (_Nodes == null || FunctionToCall == null)
      {
        return;
      }
      foreach (System.Windows.Forms.TreeNode Child in _Nodes)
      {
        RecurseTree(Child.Nodes, FunctionToCall);
        FunctionToCall(Child);
      }
    }
    public delegate void ProcessNodeDelegate(System.Windows.Forms.TreeNode _Node);

    public static void SortTreeView(System.Windows.Forms.TreeView _TreeView)
    {
      RecurseTree(_TreeView.Nodes, new ProcessNodeDelegate(SortNodes));
    }

    public static void SortTreeNodes(TreeNodeCollection _Nodes)
    {
      for (int i = 0; i < _Nodes.Count - 1; i++)
      {
        for (int j = _Nodes.Count - 1; j > i; j--)
        {
          if (((IComparable)_Nodes[j - 1].Tag).CompareTo(_Nodes[j].Tag) >= 0)
          {
            TreeNode tmp = _Nodes[j];
            _Nodes[j] = _Nodes[j - 1];
            _Nodes[j - 1] = tmp;
          }
        }
      }
    }
0
Comment
Question by:directxBOB
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
Kate12 earned 500 total points
Comment Utility
you can use:

TreeView.Sort();

In case you want to control de sorting of the tree you'll have to use the TreeView property "TreeViewNodeSorter" which implements a IComparer.

treeView.TreeViewNodeSorter = new NodeSorter();

// Node sorter
class NodeSorter : IComparer
{
    public int Compare(object x, object y)
    {
        TreeNode nodeX = (TreeNode)x;
        TreeNode nodeY = (TreeNode)y;
        return nodeX.Text.CompareTo(nodeY.Text);
    }
}
HTH
0
 

Author Comment

by:directxBOB
Comment Utility
Do I not require a cast for this?

treeView.TreeViewNodeSorter = new NodeSorter();
0
 
LVL 3

Expert Comment

by:Kate12
Comment Utility
No casting is needed because the NodeSorter implements the IComparer.
But again, try using the TreeView.Sort() without creating a  custom TreeViewNodeSorter. Create a custom one if your sorting is more complex than the example I posted (NodeSorter).
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now