仅在填充后重新排序树视图节点 [0]

发布于 2024-12-05 20:53:51 字数 951 浏览 1 评论 0原文

我有以下树:

Animals
 |
 |___Zebra
 |    |__Head
 |    |__Arms
 |    |__Legs
 |   
 |___Monkey
      |__Head
      |__Arms
      |__Legs

每个动物都有一个 ID 号存储在“标签”字段中,它们的名称位于节点的“名称”字段中。我想按一个显示“按 ID 排序”的按钮,并将上面的“斑马”变成“ 14" 等,然后按数字排序。然而,我希望孩子们保持头、手臂、腿的顺序。当我使用下面的代码时,它可以工作,但它也会将头臂腿重新排序为臂、头、腿。我尝试过 NodeSorter,但没有得到任何不同的结果。我对 C# 也很陌生,所以我可能错误地实现了它。 :) 我还使用带有一些额外字段的自定义节点来存储数据和布尔值。这就是下面所指的“JacksNode”。

这是代码:

public static void sortByAnimalID(TreeView tv)
    {
        tv.BeginUpdate();
        foreach (TreeNode treeNode in tv.Nodes[0].Nodes)
        {
            if (((JacksNode)treeNode).IsAnimal)
            {
                treeNode.Text = Convert.ToString(treeNode.Tag);
                treeNode.Name = Convert.ToString(treeNode.Tag);
            }
        }
        tv.Sort();
        tv.EndUpdate();
    }

对我做错了什么有什么想法吗?我已经在网上搜索了两周,所有的树视图文章都让我不知所措。然而,没有一个如此具体。 感谢伙计们/女士们的任何建议。

I have the following tree:

Animals
 |
 |___Zebra
 |    |__Head
 |    |__Arms
 |    |__Legs
 |   
 |___Monkey
      |__Head
      |__Arms
      |__Legs

Each animal has an ID number stored in the Tag field and their name is in Name field of node.I want to press a button that says "Sort by ID" and have the "Zebra" above turn into "14" etc, and then resort numerically. However, I want the children to stay with the head, arms, legs order. When I use the following code, it works, but it also re-sorts the head arms legs into arms, head, legs. I've tried a NodeSorter, but I just didn't get any different results. I'm also very new to C# so I might have implemented it incorrectly. :) I'm also using a custom node with a few extra fields to store data and boolean values. That's what the "JacksNode" refers to below.

Here's the code:

public static void sortByAnimalID(TreeView tv)
    {
        tv.BeginUpdate();
        foreach (TreeNode treeNode in tv.Nodes[0].Nodes)
        {
            if (((JacksNode)treeNode).IsAnimal)
            {
                treeNode.Text = Convert.ToString(treeNode.Tag);
                treeNode.Name = Convert.ToString(treeNode.Tag);
            }
        }
        tv.Sort();
        tv.EndUpdate();
    }

Any ideas on what I'm doing wrong? I've searched the web for two weeks and have been overwhelmed with all the treeview articles. However, none have been this specific.
Thanks guys/gals for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-12-12 20:53:51

使用 TreeNode.Level 属性来了解如何比较节点属性。像这样:

    private void SortButton_Click(object sender, EventArgs e) {
        if (treeView1.TreeViewNodeSorter == null) {
            treeView1.TreeViewNodeSorter = new NodeSorter();
        }
    }
    private class NodeSorter : System.Collections.IComparer {
        public int Compare(object x, object y) {
            TreeNode node1 = (TreeNode)x;
            TreeNode node2 = (TreeNode)y;
            if (node1.Level == 1) {
                return Convert.ToInt32(node1.Tag).CompareTo(Convert.ToInt32(node2.Tag));
            }
            else {
                return node1.Index.CompareTo(node2.Index);
            }
        }
    }

Use the TreeNode.Level property to figure out how to compare node properties. Like this:

    private void SortButton_Click(object sender, EventArgs e) {
        if (treeView1.TreeViewNodeSorter == null) {
            treeView1.TreeViewNodeSorter = new NodeSorter();
        }
    }
    private class NodeSorter : System.Collections.IComparer {
        public int Compare(object x, object y) {
            TreeNode node1 = (TreeNode)x;
            TreeNode node2 = (TreeNode)y;
            if (node1.Level == 1) {
                return Convert.ToInt32(node1.Tag).CompareTo(Convert.ToInt32(node2.Tag));
            }
            else {
                return node1.Index.CompareTo(node2.Index);
            }
        }
    }
∝单色的世界 2024-12-12 20:53:51
//bubble sort
    public  void Sort_TV_ByTag(TreeNodeCollection treeNodeCollection)
    {

        int i, j;
        int n = treeNodeCollection.Count;

        for (i = 0; i < n; i++)
        {
            for (j = 1; j < (n - i); j++)
            {
                int firstValue = int.Parse(treeNodeCollection[j - 1].Tag.ToString());
                int secondValue = int.Parse(treeNodeCollection[j].Tag.ToString());

                //you can compare by Tag , Text , anything 
                if (firstValue > secondValue)
                {

                    //swap the nodes
                    TreeNode n1 = treeNodeCollection[j - 1];
                    TreeNode n2 = treeNodeCollection[j];
                    treeNodeCollection.Remove(n1);
                    treeNodeCollection.Remove(n2);
                    treeNodeCollection.Insert(j, n1);
                    treeNodeCollection.Insert(j - 1, n2);

                }

            }
        }

    }


    private void button1_Click(object sender, EventArgs e)
    {
        Sort_TV_ByTag(treeView1.Nodes[0].Nodes);
    }
//bubble sort
    public  void Sort_TV_ByTag(TreeNodeCollection treeNodeCollection)
    {

        int i, j;
        int n = treeNodeCollection.Count;

        for (i = 0; i < n; i++)
        {
            for (j = 1; j < (n - i); j++)
            {
                int firstValue = int.Parse(treeNodeCollection[j - 1].Tag.ToString());
                int secondValue = int.Parse(treeNodeCollection[j].Tag.ToString());

                //you can compare by Tag , Text , anything 
                if (firstValue > secondValue)
                {

                    //swap the nodes
                    TreeNode n1 = treeNodeCollection[j - 1];
                    TreeNode n2 = treeNodeCollection[j];
                    treeNodeCollection.Remove(n1);
                    treeNodeCollection.Remove(n2);
                    treeNodeCollection.Insert(j, n1);
                    treeNodeCollection.Insert(j - 1, n2);

                }

            }
        }

    }


    private void button1_Click(object sender, EventArgs e)
    {
        Sort_TV_ByTag(treeView1.Nodes[0].Nodes);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文