将TreeNode移动到顶部,上部,下部和底部C#

时间:2016-07-14 07:21:11

标签: treeview treenode

我对树视图知之甚少。我希望能够将树节点移动到顶部并向上移动树节点。

-Fruits   -苹果   -Oranges   -香蕉   -Watermelon

以下是我尝试实施的代码。

首先,我将西瓜移到顶部,它工作正常。接下来,当我尝试向上移动香蕉时,香蕉的指数将显示-1。还有什么我做错了吗?

    private void moveTopToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TreeNode tv = new TreeNode();
        TreeNode selectedNodeToMove = new TreeNode();

        try
        {
            TreeNode parent = treeView.SelectedNode.Parent;

            selectedNodeToMove = treeView.SelectedNode;

            if (parent != null)
            {
                parent.Nodes.Remove(treeView.SelectedNode);
                parent.Nodes[0] = selectedNodeToMove;
            }
        }

        catch (Exception ex)
        {

        }
    }

    private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TreeNode tv                 = new TreeNode();
        TreeNode selectedNodeToMove = new TreeNode();

        try
        {
            TreeNode parent = treeView.SelectedNode.Parent;

            selectedNodeToMove = treeView.SelectedNode;

            if (parent != null)
            {
                int index = parent.Nodes.IndexOf(treeView.SelectedNode);

                if (index > 0)
                {
                    tv = parent.Nodes[index - 1];

                    parent.Nodes.Remove(treeView.SelectedNode);
                    parent.Nodes.Remove(tv);
                    parent.Nodes.Insert(index - 1, selectedNodeToMove);
                    parent.Nodes.Insert(index, tv);
                }
            }
        }

        catch (Exception ex)
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

我设法得出答案,它正在发挥作用。如果有人在看这个,只想分享。

    private void moveTopToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TreeNode selectedNodeToMove = new TreeNode();

        try
        {
            int index           = -1;
            TreeNode parent     = treeView.SelectedNode.Parent;
            TreeNode node= this.treeView.SelectedNode.Clone() as TreeNode;

            if (parent != null)
            {
                for (int j = 0; j < this.treeView.SelectedNode.Parent.Nodes.Count; j++)
                {
                    if (this.treeView.SelectedNode == this.treeView.SelectedNode.Parent.Nodes[j])
                    {
                        index = j;
                        break;
                    }
                }

                this.treeView.BeginUpdate();
                this.treeView.SelectedNode.Parent.Nodes.Insert(0, node);
                this.treeView.SelectedNode.Parent.Nodes.RemoveAt(index + 1);
                this.treeView.EndUpdate();

                this.treeView.SelectedNode = node;
                this.treeView.Select();

            }

        }

        catch (Exception ex)
        {

        }
    }

    private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TreeNode tv                 = new TreeNode();
        TreeNode selectedNodeToMove = new TreeNode();

        try
        {
            TreeNode parent= treeView.SelectedNode.Parent;
            TreeNode node= this.treeView.SelectedNode.Clone() as TreeNode;

            if (parent != null)
            {
                int index = -1;

                for (int j = 0; j < parent.Nodes.Count; j++)
                {
                    if (this.treeView.SelectedNode == parent.Nodes[j])
                    {
                        index = j;
                        break;
                    }
                }

                this.treeView_.BeginUpdate();
                this.treeView.SelectedNode.Parent.Nodes.Insert(index - 1, node);
                this.treeView.SelectedNode.Parent.Nodes.RemoveAt(index + 1);
                this.treeView.EndUpdate();

                this.treeView.SelectedNode = node;
                this.treeView.Select();
        }

        catch (Exception ex)
        {
        }
    }

    private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
    {
        TreeNode tv                 = new TreeNode();
        TreeNode selectedNodeToMove = new TreeNode();

        try
        {
            TreeNode parent = treeView.SelectedNode.Parent;
            TreeNode node = this.treeView.SelectedNode.Clone() as TreeNode;
            selectedNodeToMove = treeView.SelectedNode;

            if (parent != null)
            {
                int index = -1;

                for (int j = 0; j < parent.Nodes.Count; j++)
                {
                    if (this.treeView.SelectedNode == parent.Nodes[j])
                    {
                        index = j;
                        break;
                    }
                }

                this.treeView.BeginUpdate();
                this.treeView.SelectedNode.Parent.Nodes.RemoveAt(index);
                this.treeView.SelectedNode.Parent.Nodes.Insert(index + 1, node);
                this.treeView.EndUpdate();

                this.treeView.SelectedNode = node;
                this.treeView.Select();

        }

        catch (Exception ex)
        {

        }
    }

    private void moveBottomToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            TreeNode parent = treeView.SelectedNode.Parent;
            TreeNode node  = this.treeView.SelectedNode.Clone() as TreeNode;

            if (parent != null)
            {
                int index = -1;

                for (int j = 0; j < parent.Nodes.Count; j++)
                {
                    if (this.treeView.SelectedNode == parent.Nodes[j])
                    {
                        index = j;
                        break;
                    }
                }

                if (index != parent.Nodes.Count - 1)
                {
                    this.treeView.BeginUpdate();
                   this.treeView.SelectedNode.Parent.Nodes.Insert(parent.Nodes.Count, node);
                    this.treeView.SelectedNode.Parent.Nodes.RemoveAt(index);
                    this.treeView.EndUpdate();

                    this.treeView.SelectedNode = node;
                    this.treeView.Select();
        }

        catch (Exception ex)
        {

        }
    }