Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Context Menu& TreeView

Context Menu& TreeView

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krieg38
    wrote on last edited by
    #1

    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.

    T S K 3 Replies Last reply
    0
    • K krieg38

      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.

      T Offline
      T Offline
      tarasn
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K krieg38

        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.

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K krieg38

          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.

          K Offline
          K Offline
          krieg38
          wrote on last edited by
          #4

          Thx:) I didn`t knew about the treeView1.GetNodeAt method

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups