Treeview question
-
Hello All, I have an app which has a treeview, and I would like to perform a unique action when the user double clicks a leaf node of the tree. I handled the double click event, but this doesn't tell me much. How do I get to the actual tree node (if any) that the user double clicked on? Thanks
-
Hello All, I have an app which has a treeview, and I would like to perform a unique action when the user double clicks a leaf node of the tree. I handled the double click event, but this doesn't tell me much. How do I get to the actual tree node (if any) that the user double clicked on? Thanks
-
Thanks. But, the problem that I am having is that the dobule click handler is called anytime the user double clicks anywahere in the tree, whether or not the user actually double clicked on a tree node. If I click off in the empty space of the control, the handler is called. The selected node property has a node in it, but I don't have any way to know if that node was actually double clicked. Aaron
-
Thanks. But, the problem that I am having is that the dobule click handler is called anytime the user double clicks anywahere in the tree, whether or not the user actually double clicked on a tree node. If I click off in the empty space of the control, the handler is called. The selected node property has a node in it, but I don't have any way to know if that node was actually double clicked. Aaron
Try handling the
AfterSelect
event instead. You can do something like the following:private void treeView1\_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { TreeNode node = e.Node as TreeNode; if(node != null) { MessageBox.Show(node.Text); } }
- Nick Parker Microsoft MVP - Visual C#
My Blog | My Articles -
Thanks. But, the problem that I am having is that the dobule click handler is called anytime the user double clicks anywahere in the tree, whether or not the user actually double clicked on a tree node. If I click off in the empty space of the control, the handler is called. The selected node property has a node in it, but I don't have any way to know if that node was actually double clicked. Aaron
The
GetNodeAt
[^] member will tell you the node at a specific point location. But looking at the docs forDoubleClick
[^] event, theTreeView
is not supposed to raise aClick
orDoubleClick
event if the click is not over a node (see below the table before the example.) -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
The
GetNodeAt
[^] member will tell you the node at a specific point location. But looking at the docs forDoubleClick
[^] event, theTreeView
is not supposed to raise aClick
orDoubleClick
event if the click is not over a node (see below the table before the example.) -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
Well, the documentation says that it will not, but if I set a breakpoint in the handler for the double click event, and double click somewhere off in the margin of the control (not over a node), the handler is called. The double click event does not contain any mouse location info to use with GetNodeAt. I suppose maybe I could also have a handler for MouseDown, or MouseUp, and save these coordinates for use in the handler for double click, but that seems kind of ugly.
-
Try handling the
AfterSelect
event instead. You can do something like the following:private void treeView1\_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { TreeNode node = e.Node as TreeNode; if(node != null) { MessageBox.Show(node.Text); } }
- Nick Parker Microsoft MVP - Visual C#
My Blog | My ArticlesYes, but this will be called if I single click the tree node, won't it? Basically, I need to perform two different actions if the user double clicks a tree node, and if they simply select it (by moving the keyboard, or clicking on it with the mouse). If the double click event args had some more info in them, like mouse coordinates, it would help me figure out if the user actually double clicked a node, or off in space. My only other thought is to also handle mouse down events, save the corrdinates from these, and use them in the handler to doube click to figure out where the double click happened. This seems pretty messy, surely, there's a cleaner way to do this?