Treeview(每个下一个数字应为上一个数字的子代 [英] Treeview(every next number should be child of Previous number

查看:81
本文介绍了Treeview(每个下一个数字应为上一个数字的子代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为mystack的堆栈,在mystack中,我按了四个数字(5,2,1,0),当我弹出mystack时,它很可能会显示(0,1,2,5),现在我想在这样的树状视图中显示这样,每个下一个数字应为上一个数字的子元素

0
1
2
5

请事先帮助我

I have a stack named mystack,in mystack i push four numbers(5,2,1,0) and when i pop mystack it ofcoursly show(0,1,2,5) now i want to make treeview in such way that every next number should be child of previous number like this

0
1
2
5

kindly help me thanks in advance

推荐答案

用代码说比解释还好:
It''s better to say in code than explaining:
class Form1
{
    // Variable to remember last used TreeNode
    TreeNode lastUsedTreeNode = null;

    private AddNewNode(int newNumber)
    {
        // Create the newly to append TreeNode
        TreeNode newTreeNode = new TreeNode(newNumber.ToString());

        if(lastUsedTreeNode == null)
        // Append node to TreeView as root
        {
            treeView1.Nodes.Add(newTreeNode);
        }
        else
        // Append to last used TreeNode
        {
            lastUsedTreeNode.Nodes.Add(newTreeNode);
        }

        // Remember last used node for next time
        lastUsedTreeNode = newTreeNode;
    }
}


伪:
TreeNode parent = null, node;
int value;
while(stack.Count > 0){
 node = new TreeNode(stack.Pop().ToString());
 parent.Nodes.Add(node);
 parent = node;
}



即保留对上一个节点的引用,并在每个步骤中将当前节点添加到该节点,并将其更新为刚刚添加的节点.

已更新,更像是实际代码(如何将节点添加为另一个已更新节点的子节点)



i.e. keep a reference to the previous node, and each step, add the current node to it and update it to be the node you just added.

edit: updated to be a bit more like real code (how to add a node as a child of another updated)


root
  1       treeView1.Nodes[0].Nodes.Add(1);//to add under root root is already added
     
     2      treeView1.Nodes[0].Nodes[0].Nodes.Add(2);//add under 1
     

         3   treeView1.Nodes[0].Nodes[0].Nodes[0].Nodes.Add(3);//add under 2



我花了很多时间,但是按照上面想要的那样在层次结构中添加节点是我认为的方式,如果您知道要添加哪些节点的深度会更好;

@BobJanova这是您使树节点成为另一个子节点的方式;
请继续动态尝试此操作,而对深度没有任何限制
例如:-



Spent lot of time on this but to add node in the hierarchy as you want above is the way I think if you know the depth up to which nodes are to be added it would be better;

@BobJanova this is how you make a tree node a child of another;
pls keep trying to this dynamically without any limit on depth
Eg:-

if(treeView1.Nodes.Count==1)
{
treeView1.Nodes[0].Nodes.Add(1);
}


我们需要找到代码如何将下一个节点更改为treeView1.Nodes [0] .Nodes [0] .Nodes.Add(2);


祝你好运.........


We need to find how for next node the code changes to treeView1.Nodes[0].Nodes[0].Nodes.Add(2);


Best of luck.........


这篇关于Treeview(每个下一个数字应为上一个数字的子代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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