Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView Node Single-Click

Status
Not open for further replies.

andegre

MIS
Oct 20, 2005
275
US
Hi All, I have a TreeView control that has a 2 different levels of nodes (parents plus children). I want one event to fire in a single mouse-click, and a different event to fire on a double mouse-click. Double works just fine, but on the single, it does not recognize which level of the tree was clicked on the FIRST try.

For example, when starting the program, if I click on the parent node, it returns me a nodelevel of 0 (zero), which is correct. If I click on the child right after that, it returns 0 again. But if I click on any other child one more time, then it finally returns 1.

Does anyone know why it takes two single clicks to realize which level was clicked? Is there some other event that I need to fire in order for it to realize which level was clicked?

Thanks in advance.

FYI - I'm using VS 2008 in C#
 
Each node also has a Parent property which points to the parent node. You could check that - with only two levels of nodes, if the Parent is null, the node is a parent, otherwise it's a child.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
hilfy, it works the second time I click on the node, just not the first. It's like the node doesn't realize that I just clicked on it the first time, even though it's firing the mouse-click event.

ie when I start the program, and immediately click the parent, that works because it doesn't realize that I clicked the node.

but...

when I start the program, and immediately click the child node, it returns a node level 0 (the parent). But if I click the ANY child node after clicking a child node before, it then returns the correct node level of 1.

It's like it's telling me what the node level was BEFORE the mouse click.

 
Sometimes to get around these "quirks" you have to try something else. Check the Parent property like I described and see if that makes a difference.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
I see what you're saying now...have it process something else to hopefully make the clicked node "register" before requesting which node was clicked. I'll try that.
 
Didn't work.

The first node that I selected at startup was the child node, and in the code I added:

TreeNode parentNode = tvMain.SelectedNode.Parent;
if (parentNode != null)
{
int parentNodeLevel = parentNode.Level;
}

It never hit the code because parentNode was null.

Any other suggestions?
 
Hmm...that's odd. I use Parent frequently when I'm setting/clearing check marks and I've never had a problem with it not giving me information.

Have you checked to see if SelectedNode is null?

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
if it helps, i did NOT use the treeview that includes check marks.
 
I changed the parentNode variable to get the SelectedNode, and it was equal to the parentNode (the incorrect one).
 
there are several events in the treeview, be sure to use the "AfterChanged" event. If you use click, it will still show the selectedNode property as the previous node (not the one you just cliked on because it hasnt been set to the selected node yet)

But then again if you click on a node that is ALREADY selected, it wont fire the afterchanged event because the selected node hasnt changed. If thats a problem i might have another solution for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top