Treenode selection confusing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a tree node called Sales with two child nodes, Persons and Products. There are also two context menus, one for Sales and one for Persons/Products.When I select Sales with left click its ok. After I have selected Sales, I right-click a child node but the context menu of the Sales appears. That is the selection of the tree node remains to the previous one
 
Hi,

You have not included the code to show what u are doing. However I guess you must be changing the context menu in the AfterSelect event of the treeview. However the selectednode property does not change when we right click on the node. So u will have to set the selectednode to the 1 which u have rightclicked in the mousedown event . Now the code in the AfterSelect event will work.
Here is a sample code listing:

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
if(treeView1.SelectedNode.Text=="Sales")
treeView1.ContextMenu=salesMenu;
else
treeView1.ContextMenu=childMenu;
}

private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(treeView1.GetNodeAt(e.X, e.Y)!=null)
{ treeView1.SelectedNode= treeView1.GetNodeAt(e.X, e.Y);
}
}

If u do not wish to change the SelectedNode on right click just move the code where u are assigning the context menu in AfterSelect to MouseDown event.
 
Back
Top