向 TreeNode 添加或删除级别 [英] Adding or Removing a level to a TreeNode

查看:36
本文介绍了向 TreeNode 添加或删除级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用 Windows 窗体中的 TreeView 添加或删除级别?

I was wondering if it was possible with a TreeView in a windows form to add or remove a level?

例如:我的树视图开始是这样的:

For example: my treeview is like this to begin with:

ParentNode  
|    Child1  
|    Child2  

如果用户点击一个按钮向 Child2 添加一个关卡,它会变成:

if user clicks on a button to add a level to Child2 it becomes:

ParentNode   
|    Child1  
|    |    Child1.1  

有一个 Node.Level 函数,但只能用于获取级别而不能设置它.

There is a a Node.Level function but only usable to get the level and not to set it.


节点是自动构建的,级别是根据 Excel 单元格的样式分配的.问题是,碰巧创建的节点不在正确的位置,因为excel文件没有做好.所以我看到 2 个选项来解决这个问题:


The nodes are built automatically, the level is assigned depending on the style of an excel cell. The problem is, it happens that the created node is not at it's correct place because the excel file is not well made. So I see 2 options o resolve this problem:

1-用户直接修改excel文件
2- 我在选择的节点上创建了一个 Move Left Move Right 按钮.

1- the user modifies the excel file directly
2- I create a Move Left Move Right button on a selection of nodes.

我想提供第二种可能性.

I'd like to offer the 2nd possibility.

这是我用来构建节点的代码:

Here's the code I used to build the nodes:

public static void AddNodes(Excel.Application app,
                                    TreeView treeView)
    {
        Excel.Range selection = app.Selection;

        ArrayList style = new ArrayList();

        TreeNode parentNode = treeView.SelectedNode;

        //Selected Node => Last used node
        for (int i = 1; i <= selection.Rows.Count; i++)
        {
            TreeNode tn;

            int fontSize = Convert.ToInt32(selection.Cells[i].Font.Size);

            if (!style.Contains(fontSize))
            { 
                style.Add(fontSize); 
            }

            else if (style[style.Count - 1].Equals(fontSize))
            {
                try
                { 
                    treeView.SelectedNode = treeView.SelectedNode.Parent; 
                }
                catch (Exception x)
                { 
                    ErrorBox(x); 
                }
            }

            else
            {
                int indexPreviousCellofSameColor = style.IndexOf(fontSize);

                //Select TN parent
                for (int j = 1; j <= (style.Count - indexPreviousCellofSameFont); j++)
                { treeView.SelectedNode = treeView.SelectedNode.Parent; }

                style.RemoveRange(indexPreviousCellofSameFont + 1, style.Count - indexPreviousCellofSameFont - 1);
            }

            if (selection.Cells[i].Value2 == null)
            {
                //if empty cell, do something ... or nothing
                treeView.SelectedNode = treeView.SelectedNode.LastNode;
            }
            else
            {
                //Add new TN to parent - TN object corresponds to excel cell
                tn = new TreeNode()
                {
                    Text = selection.Cells[i].Value2,
                    Tag = selection.Cells[i],
                };
                treeView.SelectedNode.Nodes.Add(tn);
                tn.ToolTipText = tn.Level.ToString();

                //selected TN => created TN
                treeView.SelectedNode = tn;
            }
        }
    }

推荐答案

我不得不将我的答案完全更改为更改后的问题.这似乎在我的测试中完成了工作.它将所选节点移动到一个新级别,位于其正上方的级别之下.它需要更多的检查,以确保您不会将节点移动到遗忘......

I had to change my answer completely to the changed question. This seems to do the job in my tests. It moves the selected node to a new level, under the one that was just above it. It needs more checks offcourse to make sure your not moving nodes to oblivion...

private void button1_Click(object sender, EventArgs e)
{
    TreeNode selected = treeViewFilter.SelectedNode;
    TreeNode parent = selected.Parent;

    // find the node just above the selected node
    TreeNode prior = parent.Nodes[selected.Index - 1];

    if (parent != prior)
    {
        treeViewFilter.Nodes.Remove(selected);
        prior.Nodes.Add(selected);
    }
}

这篇关于向 TreeNode 添加或删除级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆