TreeView AfterSelect Does not fire on Right click [ C# ]

V

vijaynats

I have a treeview with a ContextMenu attached. When i click on a node,
AfterSelect fires but does not fire when right clicked (the context
menu pops up).

(Background: I have loaded a list of disk file names onto the tree and
have to open the file thru the contextmenu for editing. It opens the
file whose treeview node was selected earlier using a left mouse
click).

Any solutions?

Thanks!

Vijay
 
F

Frans Bouma [C# MVP]

I have a treeview with a ContextMenu attached. When i click on a node,
AfterSelect fires but does not fire when right clicked (the context
menu pops up).

(Background: I have loaded a list of disk file names onto the tree and
have to open the file thru the contextmenu for editing. It opens the
file whose treeview node was selected earlier using a left mouse
click).

That's a bug in the current treeview. Create a routine which sets the
context menu accordingly to the current selected node.

Then, in the mousedown event (!), check if the button is the right
one, if so, first set the selected node and then call your contextmenu
set routine like this:

private void _mainTreeView_MouseDown(object sender, MouseEventArgs e)
{
// select the node the mouse is over through code. The select event is
not fired if its the same
// node. Only act if it's the RMB.
if(e.Button != MouseButtons.Right)
{
return;
}

// Store the selected node (can deselect a node).
_mainTreeView.SelectedNode = _mainTreeView.GetNodeAt(e.X,e.Y);

// set the right contextmenu with the selected node.
SetNodeSpecificSettings();
}

In afterselect, you too call your context menu specific settings
routine.

Frans


--
 
M

Mark Broadbent

Not convinced it's a bug, since that behaviour is still present in framework
2.0b2.
I don't think the afterselect fires (at all) if right mouse button clicked ,
so the last part of your solution would not be implemented there - in fact I
would have thought that he would implement that part from the context menu
item click events.

Br,

Mark.
 
V

vijaynats

I tried out the above and it works just fine. Now instead of the
automatic ContextMenu i display the ContextMenu using the Show method.

Thanks a lot!

vijay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top