Events of ContextMenu of a Treeview when menu is shown.

  • Thread starter Thread starter Norm Skinner
  • Start date Start date
N

Norm Skinner

I have a form with a treeView control that displays a contextMenu when
the user right clicks on a node in the tree. I'm having a hard time
figuring out how to resolve a problem. After the user right clicks on
a TreeNode, causing the code to be run below, the contextMenu is
displayed. This is ok. The problem I'm having is when I test the
following scenario. After the contextmenu is displayed, the user
doesn't want to click a menu item, but click on a different node in
the treeview control. The next click on the tree doesn't create a
click event for the treeview. Instead, it just moves the contextMenu
to the spot where the user clicked. The kind of action I assumed
would happend, is the contextMenu would go away, and the treeviews
mousedown event fire. Can anyone suggest the best way to accomplish
this task? I've been looking at events for the both the treeView and
contextMenu, and nothing seems to be an obvious choice.

One funny thing I've noticed is that if the user clicks off the menu a
second time, the desired action occurs. It's just that first click
after the contextMenu is displayed that acts funny.

private void treeIIS_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point clickPoint = new Point(e.X,e.Y) ;
TreeNode node = treeIIS.GetNodeAt(clickPoint);
if(node != null)
{
// Highlight the node that the user clicked.
//The node is highlighted until the Menu is displayed on the screen
treeIIS.SelectedNode = node;
Point screenPoint = treeIIS.PointToScreen(clickPoint);
Point formPoint = this.PointToClient(screenPoint);
mnuContextTree.Show(this, formPoint);
}
}
}
 
If you create a context menu and add it as to the ContextMenu property of
the TreeView then that is exactly what happens - if you click off the
context menu it dissappears.

Br,

Mark.
 
Back
Top