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