Click here to Skip to main content
16,005,038 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 20-Oct-11 1:22am
v3

It's better to say in code than explaining:
C#
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;
    }
}
 
Share this answer
 
v2
Comments
bilawal121 20-Oct-11 8:36am    
System.Windows.Forms.TreeNode' does not contain a definition for 'Add'
lukeer 20-Oct-11 9:28am    
Did I say TreeNode.Add()? What I meant was TreeNode.Nodes.Add() just as 5 lines above.
bilawal121 21-Oct-11 6:26am    
Thanku sir, this helps me alot :)
Pseudo:
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)
 
Share this answer
 
v2
Comments
bilawal121 20-Oct-11 8:20am    
It gives an error of "Property or indexer 'System.Windows.Forms.TreeNode.Parent' cannot be assigned to -- it is read only

how i remove this?????
bilawal121 20-Oct-11 8:33am    
it only adds the node
like this
0
1
2
5
BobJanova 20-Oct-11 8:52am    
It's pseudocode, you can look up how you make a tree node a child of another just as well as I can.

ed - seems it's parent.Nodes.Add(node)
C#
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


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:-
C#
if(treeView1.Nodes.Count==1)
{
treeView1.Nodes[0].Nodes.Add(1);
}

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


Best of luck.........
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900