Context Menu& TreeView
-
Is there a way to display different context menus(or with different submenus) for a treee view for different nodes. Foe example if i have too roots and I want to display a different menu for the child nodes of the forst root, other than for the children of the seccond root. I have tryed with after select or mouse up event but I want also to retrieve the selected node that generated the menu but it does`n works.
-
Is there a way to display different context menus(or with different submenus) for a treee view for different nodes. Foe example if i have too roots and I want to display a different menu for the child nodes of the forst root, other than for the children of the seccond root. I have tryed with after select or mouse up event but I want also to retrieve the selected node that generated the menu but it does`n works.
Here's code snippet that shows different context menus for selected tree node. private void Form1_Load(object sender, System.EventArgs e) { // create the root node TreeNode treeNodeRoot = new TreeNode("Root"); treeNodeRoot.Tag = "root"; // create first child node TreeNode treeNodeChild1 = new TreeNode("Child1"); treeNodeChild1.Tag = "child"; treeNodeRoot.Nodes.Add( treeNodeChild1 ); // create second child node TreeNode treeNodeChild2 = new TreeNode("Child2"); treeNodeChild2.Tag = "child"; treeNodeRoot.Nodes.Add( treeNodeChild2 ); treeView1.Nodes.Add( treeNodeRoot ); } /// /// Create context menu /// /// tag value /// private ContextMenu GetContextMenuByTag(string tag) { ContextMenu contextMenu = new ContextMenu(); if( tag.StartsWith("child") ) { contextMenu.MenuItems.Add("Child menu item"); } else { contextMenu.MenuItems.Add("Root menu item"); } return contextMenu; } private void treeView1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button==MouseButtons.Right) { // retrive node or use treeView1.SelectedNode TreeNode theNode= treeView1.GetNodeAt(e.X, e.Y); if (theNode!=null) { if( theNode.Tag!=null ) { ContextMenu contextMenu = GetContextMenuByTag((string)theNode.Tag); contextMenu.Show( treeView1, new Point(e.X, e.Y) ); } } } } DevIntelligence.com - My blog for .Net Developers -- modified at 3:16 Monday 9th January, 2006
-
Is there a way to display different context menus(or with different submenus) for a treee view for different nodes. Foe example if i have too roots and I want to display a different menu for the child nodes of the forst root, other than for the children of the seccond root. I have tryed with after select or mouse up event but I want also to retrieve the selected node that generated the menu but it does`n works.
Subscribe to the MouseDown event of the Treeview control. In the event handler, code it like
if (e.Button == MouseButtons.Right)
{
Point point = new Point(e.X, e.Y);
TreeNode node = treeView.GetNodeAt(point);
if (node == null)
return;treeView.SelectedNode = node;
if (node.Tag is TypeX)
{
XContextMenu.Show(treeView, point);
}
else if (node.Tag is TypeY)
{
YContextMenu.Show(treeView, point);
}
}Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Is there a way to display different context menus(or with different submenus) for a treee view for different nodes. Foe example if i have too roots and I want to display a different menu for the child nodes of the forst root, other than for the children of the seccond root. I have tryed with after select or mouse up event but I want also to retrieve the selected node that generated the menu but it does`n works.