Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to set treeView.selectedNode programlly in C# and I want to be occured the beforeSelect event while proccessing this assigning. but my question is what I describe below:
when I use the code bellow, the beforeSelect event occured and call it's function.

C#
this.treeView1.SelectedNode = treeView1.Nodes[Level1Index];
this.treeView1.SelectedNode=treeView1.SelectedNode.Nodes[Level2Index];


but when I use such as bellow It doese'nt occure.

C#
this.treeView1.SelectedNode = treeView1.Nodes[Level1Index].Nodes[Level2Index];

why?
Posted
Updated 27-Mar-13 1:29am
v3
Comments
Richard MacCutchan 27-Mar-13 7:30am    
Impossible to tell, you need to provide some more information about the actual valuse of your variables at the time this code is executed.

You should assign your obtained nodes into variables so that you can see their content while debugging (F5).

C#
TreeViewNode node1 = treeView1.Nodes[Level1Index];
TreeViewNode node2 = treeView1.Nodes[Level2Index];


From here, see in the the debugger what your node1 and node2 variables contain ; you will have a better understanding about your problem.
 
Share this answer
 
Hi,
I double checked, your scenario and when the SelectedNode is set regardless of its hierarchy, it always trigger the BeforeSelected event.

It is most likely that the
C#
treeView1.Nodes[Level1Index].Nodes[Level2Index];
is null. if it is null then the node will not be selected and so the BeforeSelected event will not be triggered.

Regards
Jegan
 
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