Contextmenu & TreeView

P

Peter Zentner

Hi all,

if I assign a context menu to a treeview and popup the contextmenu with the
APP-key the context menu appears in the center of the treeview control and
not at the selected treenode. Is there a chance to change this behaviour?

Regards

Peter
 
E

Eric Promislow

I was just struggling with this exact same problem, and came up with a
solution which I like/dislike as much as the other suggestions:

private void tvX_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
TreeNode tn = tvX.GetNodeAt(e.X, e.Y);
if (tn == null) {
return;
}
// This is awkward
if (tn.ImageIndex == 2) {
tvX.ContextMenu = null;
} else {
tvX.SelectedNode = tn;
tvX.ContextMenu = ctxtMenu_tvX;
}
}
}

BTW, my setting the selected node is my solution to another reasonably
FAQ in this group (and with other UI frameworks I've used): how to map
a context menu item selection to a node in the tree.

If someone has an elegant solution to either of these problems, one that
preferably doesn't involve hooking the Win32 event loop, I would
appreciate a post.

- Eric
 

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