views:

1560

answers:

4

Hi.

I'm using the click event on the TreeView to do some stuff when a node is clicked in the TreeView. I do this by getting the node that is click on by calling GetNodeAt() with the mouse coordinates, like this:

private void TreeView_Click(object sender, System.EventArgs e)
{
    MouseEventArgs mouseEventArgs = e as MouseEventArgs;

    if (mouseEventArgs == null)
        return;

    // Get the node that is being clicked.         
    TreeNode node = this.GetNodeAt(mouseEventArgs.X, mouseEventArgs.Y);

    // Do other stuff...
}

However, the GetNodeAt() method only works when the click is on the node label, when the node image is clicked then GetNodeAt() returns null. This is a bit annoying since the node is actually selected when the image is clicked but I can't find out what node it is.

Do anyone have any suggestions?

Updated: I've gotten some suggestions to use SelectedNode instead. I can't since it's set after the Click event is fired. This actually in a control that inherits TreeView and what it does is fire it's own Clicked event but with the underlying data that the TreeNode represents instead of the TreeNode itself.

Updated: Turns out that someone had overridden the GetNodeAt() method in our code which introduced this behavior, which I didn't realize. So the question is null and void and there is no problem with the GetNodeAt() method. Why someone would do this remains a mystery. :)

+2  A: 

Why not just use TreeView.SelectedNode?

Sean Bright
I can't since it's set after the Click event is fired. This actually in a control that inherits TreeView and what it does is fire it's own Clicked event but with the underlying data that the TreeNode represents instead of the TreeNode itself.
Casper
A: 

Try:

TreeNode node = this.SelectedNode;

EDIT: Beaten to the punch by Sean

Chris Doggett
+4  A: 

Have you tried the BeforeSelect or AfterSelect Events? You can get the selected node straight from the TreeViewCancelEventArgs, then use it in your Click Event.

Edit: Additional Thought: The only problem I can see with this is that the BeforeSelect event actually fires after the Click and MouseClick Events.

Another Edit: If you need an event that fires before Click, you can use NodeMouseClick - TreeNodeMouseClickEventArgs has a Node property.

Moose
These or SelectedNodeChanged should be the events. Unless you want to only do something when a mouse clicks it, otherwise using the keyboard might break your code.
Samuel
Right, took me a while to figure out myself as well, but Treeview.AfterSelect (or BeforeSelect)is most likely what you want rather than the click event.
Davy8
@Samuel - BeforeSelect and AfterSelect fire even on a keyboard event like up arrow.
Moose
I know, I meant that you should follow Casper's approach when you only want mouse interactivity.
Samuel
Oh, sorry, I was thinking you meant this won't work when using the keyboard. Carry on then! :-)
Moose
What I'm doing here is trying to fix some old legacy code, and of course there had to be some some stuff that were relying on the order things are firing to AfterSelect and BeforeSelect broke things. NodeClicked seems to the trick though, thanks.
Casper
A: 

doesn't work on click. but sometimes, you need a handling on click