The System.Web.UI.WebControls.TreeView class offers this event, but the Forms version of TreeView doesn't. What's the equivalent in the Forms world? I'm using AfterSelect but it doesn't seem quite right. Maybe it is in fact what I'm looking for but the documentation is a bit hazy.
+2
A:
There's none in WinForms TreeView. To quote MSDN for TreeView.AfterSelect:
This event does not occur when the node is unselected. To detect this occurrence, handle the Control.MouseUp event and test the TreeNode.IsSelected property.
Yes, this sucks.
Pavel Minaev
2009-11-04 01:11:04
Control.MouseUp? They must be kidding. What if the tree is being navigating with keys instead of the mouse?
I. J. Kennedy
2009-11-04 01:13:46
Well, ok, it sucks, but you could always just extend the control and add the event yourself.
Ed Swangren
2009-11-04 01:26:19
How can you deselect a node with the keyboard?
SLaks
2009-11-04 01:32:46
The tab button I guess.
Ed Swangren
2009-11-04 01:36:07
That won't deselect a node. It'll unfocus the TreeView, but the selection will remain.
SLaks
2009-11-04 01:46:16
> How can you deselect a node with the keyboard?Press an arrow key.
I. J. Kennedy
2009-11-04 02:19:03
That will still raise the `AfterSelect` event by selecting another node.
SLaks
2009-11-04 02:53:39
+1
A:
There's nothing wrong with using AfterSelect
.
However, note that it won't fire if the selection is cleared (if SelectedNode
becomes null
) Instead, you can handle MouseUp
, as recommended in the documentation.
SLaks
2009-11-04 01:13:15