Well, never mind.  It seems this does work correctly after all.

Peter

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Peter Osucha
Sent: Thursday, May 01, 2008 9:51 AM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] CustomNodeSorter for TreeView...

I'm having trouble correctly implementing a CustomNodeSorter for a
treeview control.

I've got a TreeView control where each TreeNode has as its Tag an object
(TreeItem) that I made.  One of the properties of TreeItem is 'IsFolder'
for indicating if the TreeNode acts as a folder in the custom treeview
I'm implementing.

For nodes displayed in the treeView, I desire that nodes be arranged
such that in any branch, the folder nodes are shown first in
alphabetical order and then the non folder nodes are shown in
alphabetical order - just like in Windows Explorer.

However, I'm not implementing the CustomNodeSorter correctly.  Can
someone please point out the adjustments I need to make?

Sincerely,
Peter
------------------------------

    public class CustomNodeSorter : IComparer
    {
        /// <summary>
        /// This custom node sorter always shows a tree node with
folders first and sorted alphabetically.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public int Compare ( object x, object y )
        {
            TreeNode tx = ( TreeNode ) x;
            TreeNode ty = ( TreeNode ) y;

            TreeItem tix = ( TreeItem ) tx.Tag;
            TreeItem tiy = ( TreeItem ) ty.Tag;

            // Separate folders from other nodes.
            if ( tix.IsFolder & !tiy.IsFolder )
            {
                return 1;
            }

            if ( !tix.IsFolder & tiy.IsFolder )
            {
                return -1;
            }

            // Neither or both are folders so compare alphabetically.
            return string.Compare ( tx.Text, ty.Text );
        }
    }

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to