Win TreeView Control Issue

G

Guest

VS2005

I have a Tree control that after loading when initially displayed, all top
level nodes are collapsed. When the user expands the the node, I want to do
some processing of all the nodes under that one. So I use the AfterExpand
method. However, if the user only clicks the + to expand the node and
doesn't actually click on the node to select it, the value of Selected Node
is Nothing.

Here's the code I am trying to do in the AfterExpand routine:

Dim n As TreeNode
For Each n In treeRoles.SelectedNode.Nodes
'Do stuff here
Next

How can I accomplish this when I expect that the user will only click the +
and not actually click on the node?
 
M

Morten Wennevik [C# MVP]

VS2005

I have a Tree control that after loading when initially displayed, all top
level nodes are collapsed. When the user expands the the node, I want to do
some processing of all the nodes under that one. So I use the AfterExpand
method. However, if the user only clicks the + to expand the node and
doesn't actually click on the node to select it, the value of Selected Node
is Nothing.

Here's the code I am trying to do in the AfterExpand routine:

Dim n As TreeNode
For Each n In treeRoles.SelectedNode.Nodes
'Do stuff here
Next

How can I accomplish this when I expect that the user will only click the +
and not actually click on the node?

Take a look at the data you get from the AfterExpand event. You should get a sender (the control triggering the event) and a TreeViewEventArgs object. The TreeViewEventArgs object has a Node property which is the recently expanded node. Use this node instead.

[C#]
private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
{
foreach (TreeNode node in e.Node.Nodes)
{
// Do stuff here
}
}
 
G

Guest

Thanks Morten, that was just what I needed.

Morten Wennevik said:
VS2005

I have a Tree control that after loading when initially displayed, all top
level nodes are collapsed. When the user expands the the node, I want to do
some processing of all the nodes under that one. So I use the AfterExpand
method. However, if the user only clicks the + to expand the node and
doesn't actually click on the node to select it, the value of Selected Node
is Nothing.

Here's the code I am trying to do in the AfterExpand routine:

Dim n As TreeNode
For Each n In treeRoles.SelectedNode.Nodes
'Do stuff here
Next

How can I accomplish this when I expect that the user will only click the +
and not actually click on the node?

Take a look at the data you get from the AfterExpand event. You should get a sender (the control triggering the event) and a TreeViewEventArgs object. The TreeViewEventArgs object has a Node property which is the recently expanded node. Use this node instead.

[C#]
private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
{
foreach (TreeNode node in e.Node.Nodes)
{
// Do stuff here
}
}
 

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