Setting Treeview Index

K

Kelvin Bryant

I have tried the following code to set the index for a
node. It seems to work when I use the root node index of
0. When I try a number greater than 0 I get an out of
range error. I made sure that the tree was expanded, as
well.

treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[1];
 
N

Nicholas Paldino [.NET/C# MVP]

Kelvin,

How many nodes are there under the root? I think that the Nodes that
are being exposed is the top level of nodes, and not all of the nodes in the
tree. If you have only one node at the root, then this would definitely
fail.

Hope this helps.
 
M

Michael A. Covington

Kelvin Bryant said:
I have tried the following code to set the index for a
node. It seems to work when I use the root node index of
0. When I try a number greater than 0 I get an out of
range error. I made sure that the tree was expanded, as
well.

treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[1];

Note that treeView1.Nodes is only the list of nodes branching off the root;
not all the nodes in the entire tree.

treeView1.Nodes[1].Nodes is the list of nodes that branch off node 1, and so
forth. It's a recursive data structure.
 
G

Guest

Yes, you are on to something! My structure is as such:
Root Node
Parent Node 1
Child Node 1
Child Node 2
Child Node 3
Parent Node 2
Child Node 1
Child Node 2
Blah Blah Blah
-----Original Message-----
Kelvin,

How many nodes are there under the root? I think that the Nodes that
are being exposed is the top level of nodes, and not all of the nodes in the
tree. If you have only one node at the root, then this would definitely
fail.

Hope this helps.


I have tried the following code to set the index for a
node. It seems to work when I use the root node index of
0. When I try a number greater than 0 I get an out of
range error. I made sure that the tree was expanded, as
well.

treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[1];


.
 

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