Context menu on a TreeView
-
hi all, i have a Tree with 3 modes. each tree has 1 child. i need to create 2 context menus;one for all the nodes and the other for all the children programmatically kindly help
Sonu
Hi, have a look to my example: I created a windows-application with a form and one 'TreeView'-component named treeView1 in my code. The only code is written within the Load-event of the form:
private void Form1_Load(object sender, EventArgs e) { ContextMenu myRootContextMenu = new ContextMenu(); myRootContextMenu.MenuItems.Add("Edit Root"); ContextMenu myChild1ContextMenu = new ContextMenu(); myChild1ContextMenu.MenuItems.Add("Edit Child 1"); ContextMenu myChild2ContextMenu = new ContextMenu(); myChild2ContextMenu.MenuItems.Add("Edit Child 2"); **TreeNode rootTreeNode = new TreeNode("Root");** rootTreeNode.ContextMenu = myRootContextMenu; rootTreeNode.Nodes.Add("First Node"); rootTreeNode.Nodes[0].ContextMenu = myChild1ContextMenu; rootTreeNode.Nodes.Add("Second Node"); rootTreeNode.Nodes[1].ContextMenu = myChild2ContextMenu; treeView1.Nodes.Add(rootTreeNode); }
I created one root treenode 'rootTreeNode'TreeNode rootTreeNode = new TreeNode("Root");
and added two child nodes:rootTreeNode.Nodes.Add("First Node"); rootTreeNode.Nodes.Add("Second Node");
After that, I added this treenode to the TreeView 'treeView1' of the Form.treeView1.Nodes.Add(rootTreeNode);
Now we come to the context-menues: I created one root context-menu, and gave it a descriptionContextMenu myRootContextMenu = new ContextMenu(); myRootContextMenu.MenuItems.Add("Edit Root");
and with the property 'ContextMenu' of the rootTreeNoderootTreeNode.ContextMenu = myRootContextMenu;
I made the assignment. The context-menues for the child nodes areContextMenu myChild1ContextMenu = new ContextMenu(); myChild1ContextMenu.MenuItems.Add("Edit Child 1"); ContextMenu myChild2ContextMenu = new ContextMenu(); myChild2ContextMenu.MenuItems.Add("Edit Child 2");
and assigned in the same way. Hope this helps a little bit Regards Erik