tags:

views:

154

answers:

2

When I set SelectedNode to null the tree updates correctly but BeforeSelect and AfterSelect do not fire.

Is there any way to tell when the selection has been changed to null?

My first thought is to extend the control and add an event though I would have thought something like this would already be available.

+1  A: 

It seems the only way I could do this was to create a new control and provide a new implementation for SelectedNode, even OnAfterSelect and OnBeforeSelect weren't getting called.

public new TreeNode SelectedNode {
    get { return base.SelectedNode; }
    set {
        // Remember, if `value' is not null this will be called in `base'.
        if (value == null) {
            TreeViewCancelEventArgs args
                = new TreeViewCancelEventArgs(value, false, TreeViewAction.Unknown);
            OnBeforeSelect(args);
            if (args.Cancel)
                return;
        }
        base.SelectedNode = value;
        // Remember, if `value' is not null this will be called in `base'.
        if (value == null) {
            OnAfterSelect(new TreeViewEventArgs(value, TreeViewAction.Unknown));
        }
    }
}
Brett Ryan
Seems like a good workaround to me but the question is why didn't MS implement it like this from the beginning? Overlooking something?
Sorin Comanescu
MS overlook a lot of things Sorin ;)
Brett Ryan
+1  A: 

I think your solution is good. However, I just discovered this control (keep an eye on SO's right column :)):

http://treeviewadv.sourceforge.net/

which supports what you're looking for, maybe has other goodies too...

Sorin Comanescu
Hi Sorin, I think that is possibly a very good solution and if I hadn't already implemented my solution yesterday I would have gone with it :) However, I'm marking your solution as answered for other users who might be looking for solving the same problem, though for those looking please understand I have not yet tested this control.
Brett Ryan