-
Context Menu
I am trying to display a different context menu for different tree nodes. I can distinguish what node has been selected but the context menu is displaying the context menu for the last node selected. After searching the forums I saw that you have to place:
treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
in the mousedown event. I still have the same result. I stepped through the mouse down code to ensure it is working and it is.
*********************
On mouse up I verify the right mouse button:
*********************
if(e.Button == MouseButtons.Right)
{
*********************
I then see what type of node by checking the .Tag property:
*********************
TreeNodeTypeStruct newTreeNodeTypeStruct = new TreeNodeTypeStruct();
newTreeNodeTypeStruct = (TreeNodeTypeStruct) treeView1.SelectedNode.Tag;
if(newTreeNodeTypeStruct.getTreeNodeTypeStruct() == TreeNodeType.System)
{
*********************
I create the context menu:
*********************
MenuItem miAddZone = new MenuItem("Add Zone",
new EventHandler(OnAddZoneNode));
cm1.MenuItems.Add(miAddZone);
//EventHandler ehAddZone = new EventHandler (OnAddZoneNode);
//cm1.MenuItems.Add("Add Zone", ehAddZone);
ContextMenu = cm1;
*********************
The event method:
*********************
void OnAddZoneNode(object obj, EventArgs ea)
{
TreeNode newNode = treeView1.SelectedNode.Nodes.Add("Zone");
TreeNodeTypeStruct myTreeNodeTypeStruct = new TreeNodeTypeStruct(TreeNodeType.Zone);
newNode.Tag = myTreeNodeTypeStruct;
}
*********************
During debug the functions are stepping through but the wrong menu is being displayed.
Any advice??:(
-
Greetings,
I was able to work around that problem by showing the context menu manually, thus not setting the tree view's ContextMenu property.
Code:
TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
if (node != null)
{
MenuItem item = new MenuItem(node.FullPath);
cm1.MenuItems.Clear();
cm1.MenuItems.Add(item);
cm1.Show(treeView1, new Point(e.X, e.Y));
}
-
Captain Nuss,
Thank you so much!!!:D
I was pulling my hair out with this. It works GREAT!!
Matt