views:

264

answers:

2

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
Control.MouseUp? They must be kidding. What if the tree is being navigating with keys instead of the mouse?
I. J. Kennedy
Well, ok, it sucks, but you could always just extend the control and add the event yourself.
Ed Swangren
How can you deselect a node with the keyboard?
SLaks
The tab button I guess.
Ed Swangren
That won't deselect a node. It'll unfocus the TreeView, but the selection will remain.
SLaks
> How can you deselect a node with the keyboard?Press an arrow key.
I. J. Kennedy
That will still raise the `AfterSelect` event by selecting another node.
SLaks
+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