TreeView

W

Wndr

Hi All.
I have a treeview and I need to add the child nodes to the parent dinamicly.
This is how it looks manualy:
System.Windows.Forms.TreeNode treeNode1 = new
System.Windows.Forms.TreeNode("Node1");

System.Windows.Forms.TreeNode treeNode2 = new
System.Windows.Forms.TreeNode("Node2");

System.Windows.Forms.TreeNode treeNode3 = new
System.Windows.Forms.TreeNode("Test", new System.Windows.Forms.TreeNode[]
{treeNode1, treeNode2});

Here is my try.

//for (int i = 1; i < 3; i++)

//{

// System.Windows.Forms.TreeNode treeNode & i = new
System.Windows.Forms.TreeNode("Node" + Convert.ToString(i));

//System.Windows.Forms.TreeNode treeNode3 = new
System.Windows.Forms.TreeNode("Test", new System.Windows.Forms.TreeNode[]
{"treeNode" + Convert.ToString(i)});

//}

But it always gives me an error: "; Expected" pointing to the place right
before the & i .

Thanks in advance.
 
J

Jeff Gaines

Here is my try.

//for (int i = 1; i < 3; i++)

//{

// System.Windows.Forms.TreeNode treeNode & i = new
System.Windows.Forms.TreeNode("Node" + Convert.ToString(i));

//System.Windows.Forms.TreeNode treeNode3 = new
System.Windows.Forms.TreeNode("Test", new System.Windows.Forms.TreeNode[]
{"treeNode" + Convert.ToString(i)});

//}

But it always gives me an error: "; Expected" pointing to the place right
before the & i .

What is the '& i' meant to do? It's not needed is it?
 
W

Wndr

He Jeff
Thanks for replay.
I am getting stack with this part:

for (int i = 1; i < 3; i++)

{

string tmpName = "test_child" + Convert.ToString(i);

this.tvwTest.Nodes.AddRange(new TreeNode[]

{new TreeNode("TEST",

new TreeNode[]{

new TreeNode(tmpName)})

});

}

It adds like this:

TEST

test_child1

TEST

test_child2

and I need

Test

test_child1

test_child2

Appreciate any help.



Jeff Gaines said:
Here is my try.

//for (int i = 1; i < 3; i++)

//{

// System.Windows.Forms.TreeNode treeNode & i = new
System.Windows.Forms.TreeNode("Node" + Convert.ToString(i));

//System.Windows.Forms.TreeNode treeNode3 = new
System.Windows.Forms.TreeNode("Test", new System.Windows.Forms.TreeNode[]
{"treeNode" + Convert.ToString(i)});

//}

But it always gives me an error: "; Expected" pointing to the place right
before the & i .

What is the '& i' meant to do? It's not needed is it?
 
J

Jeff Gaines

He Jeff
Thanks for replay.
I am getting stack with this part:

for (int i = 1; i < 3; i++)

{

string tmpName = "test_child" + Convert.ToString(i);

this.tvwTest.Nodes.AddRange(new TreeNode[]

{new TreeNode("TEST",

new TreeNode[]{

new TreeNode(tmpName)})

});

}

It adds like this:

TEST

test_child1

TEST

test_child2

and I need

Test

test_child1

test_child2

Appreciate any help.

In this.tvwTest.Nodes.AddRange(new TreeNode[] you are adding a range of
nodes to this.tvwTest.Nodes.

You need to add 1 node to this.tvwTest.Nodes then add the other 2 nodes to
that new node, something like:

TreeNode tnTop = new TreeNode("Test");
this.tvwTest.Nodes.Add(tnTop);

Then add the child nodes to tnTop:

tnTop.Nodes.AddRange( etc.
 
W

Wndr

Thank you so much, this is exactly what I was asking about.

Jeff Gaines said:
He Jeff
Thanks for replay.
I am getting stack with this part:

for (int i = 1; i < 3; i++)

{

string tmpName = "test_child" + Convert.ToString(i);

this.tvwTest.Nodes.AddRange(new TreeNode[]

{new TreeNode("TEST",

new TreeNode[]{

new TreeNode(tmpName)})

});

}

It adds like this:

TEST

test_child1

TEST

test_child2

and I need

Test

test_child1

test_child2

Appreciate any help.

In this.tvwTest.Nodes.AddRange(new TreeNode[] you are adding a range of
nodes to this.tvwTest.Nodes.

You need to add 1 node to this.tvwTest.Nodes then add the other 2 nodes to
that new node, something like:

TreeNode tnTop = new TreeNode("Test");
this.tvwTest.Nodes.Add(tnTop);

Then add the child nodes to tnTop:

tnTop.Nodes.AddRange( etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top