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. Show ContextMenu in Node

Show ContextMenu in Node

Scheduled Pinned Locked Moved C#
helpquestion
3 Posts 2 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.
  • T Offline
    T Offline
    thongkk
    wrote on last edited by
    #1

    I have a treeview with the nodes inside the treeview. I would like to show the contextMenu on the node selected whenever the node is selected. Unfortunately, i can't do that because the node is not a control. Anyone out there can help, please???? Thank you Regards, cK

    S 1 Reply Last reply
    0
    • T thongkk

      I have a treeview with the nodes inside the treeview. I would like to show the contextMenu on the node selected whenever the node is selected. Unfortunately, i can't do that because the node is not a control. Anyone out there can help, please???? Thank you Regards, cK

      S Offline
      S Offline
      Schnemar
      wrote on last edited by
      #2

      put a ContextMenu on your Form private System.Windows.Forms.ContextMenu contextMenu; Example: (I used a Treeview (treeview) with 3 Nodes, every Node has one SubNode and my ContextMenu has two MenuItem's) Declare two Menu-Items for the ContextMenu: private MenuItem contextMenuItem1 = new MenuItem(); private MenuItem contextMenuItem2 = new MenuItem(); Add the following Code to your Constructor: //Initialize Context-Menu this.treeView.ContextMenu = this.contextMenu; contextMenuItem1.Text = "Action 1"; contextMenuItem2.Text = "Action 2"; this.contextMenu.MenuItems.Add(0,contextMenuItem1); this.contextMenu.MenuItems.Add(1,contextMenuItem2); contextMenuItem1.Click += new System.EventHandler(this.contextMenu_Action_1); contextMenuItem2.Click += new System.EventHandler(this.contextMenu_Action_2); Override the MouseDown Event from your Treeview Control: private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button != MouseButtons.Right) return; this.contextMenuItem1.Text = "Action 1"; this.contextMenuItem2.Text = "Action 2"; TreeNode selectedTreeNode = this.treeView.GetNodeAt(new Point(e.X,e.Y)); if(selectedTreeNode != null) { this.contextMenuItem1.Text = "Action 1 (Selected Node = " + selectedTreeNode.Text + ")"; this.contextMenuItem2.Text = "Action 2 (Selected Node = " + selectedTreeNode.Text + ")"; } } Implement the Funktions for the Eventhandler: private void contextMenu_Action_1(object sender, System.EventArgs e) { if(this.contextMenuItem1.Text == "Action 1") MessageBox.Show("Action 1 fired for Treeview"); else MessageBox.Show("Action 1 fired for selected Node in Treeview"); } private void contextMenu_Action_2(object sender, System.EventArgs e) { if(this.contextMenuItem2.Text == "Action 2") MessageBox.Show("Action 2 fired for Treeview"); else MessageBox.Show("Action 2 fired for selected Node in Treeview"); } I Hope this was helpfully for you. rgrds:) Martin

      T 1 Reply Last reply
      0
      • S Schnemar

        put a ContextMenu on your Form private System.Windows.Forms.ContextMenu contextMenu; Example: (I used a Treeview (treeview) with 3 Nodes, every Node has one SubNode and my ContextMenu has two MenuItem's) Declare two Menu-Items for the ContextMenu: private MenuItem contextMenuItem1 = new MenuItem(); private MenuItem contextMenuItem2 = new MenuItem(); Add the following Code to your Constructor: //Initialize Context-Menu this.treeView.ContextMenu = this.contextMenu; contextMenuItem1.Text = "Action 1"; contextMenuItem2.Text = "Action 2"; this.contextMenu.MenuItems.Add(0,contextMenuItem1); this.contextMenu.MenuItems.Add(1,contextMenuItem2); contextMenuItem1.Click += new System.EventHandler(this.contextMenu_Action_1); contextMenuItem2.Click += new System.EventHandler(this.contextMenu_Action_2); Override the MouseDown Event from your Treeview Control: private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button != MouseButtons.Right) return; this.contextMenuItem1.Text = "Action 1"; this.contextMenuItem2.Text = "Action 2"; TreeNode selectedTreeNode = this.treeView.GetNodeAt(new Point(e.X,e.Y)); if(selectedTreeNode != null) { this.contextMenuItem1.Text = "Action 1 (Selected Node = " + selectedTreeNode.Text + ")"; this.contextMenuItem2.Text = "Action 2 (Selected Node = " + selectedTreeNode.Text + ")"; } } Implement the Funktions for the Eventhandler: private void contextMenu_Action_1(object sender, System.EventArgs e) { if(this.contextMenuItem1.Text == "Action 1") MessageBox.Show("Action 1 fired for Treeview"); else MessageBox.Show("Action 1 fired for selected Node in Treeview"); } private void contextMenu_Action_2(object sender, System.EventArgs e) { if(this.contextMenuItem2.Text == "Action 2") MessageBox.Show("Action 2 fired for Treeview"); else MessageBox.Show("Action 2 fired for selected Node in Treeview"); } I Hope this was helpfully for you. rgrds:) Martin

        T Offline
        T Offline
        thongkk
        wrote on last edited by
        #3

        I have solved the problem. Anyway, thank you for your helpful code!!:cool:

        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