Winform Treeview 排序及图标处理

 

一、排序

1. 继承 IComparer

2.  treeView1.TreeViewNodeSorter = this;

3. 实现IComparer

public int Compare(object x, object y)
        {
            TreeNode n1 = x as TreeNode;
            TreeNode n2 = y as TreeNode;
            if (n1 == null || n2 == null)
            {
                return 0;
            }

            IUser user1 = n1.Tag as IUser;
            IUser user2 = n2.Tag as IUser;
            if (user1 == null && user2 == null)
            {
                return n1.ToolTipText.CompareTo(n2.ToolTipText);
                //return n1.Text.CompareTo(n2.Text);
            }
            if (user1 != null && user2 != null)
            {
                return user1.Name.CompareTo(user2.Name);
            }

            return user1 == null ? 1 : -1;
        }

 

一、图标

1. 如果是一个自定义的control,要生成一个dll,就直接插入图片:

image

 

2. 如果想在后面方便换图标,就动态加载:

 //load images
imageList1.Images.Add(Image.FromFile("Resource\\organization.png"));
imageList1.Images.Add(Image.FromFile("Resource\\department.png"));
imageList1.Images.Add(Image.FromFile("Resource\\None64.ico"));
imageList1.Images.Add(Image.FromFile("Resource\\user.png"));
imageList1.Images.Add(Image.FromFile("Resource\\loading.gif"));
treeView1.ImageList = imageList1;
posted @ 2014-11-09 09:37  疯吻IT  阅读(5978)  评论(0编辑  收藏  举报