This is a migrated thread and some comments may be shown as answers.

TreeView event ContextMenuOpening not executing with ContextMenuStrip

4 Answers 149 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Andrea
Top achievements
Rank 1
Andrea asked on 30 Nov 2016, 09:33 AM

Hello, if execute the attached code, the ContextMenuOpening event is not raised with ContextMenuStrip

how can I edit the contextmenu depending on the node? Can't use ContextMenu nor RadContextMenu for the purpose.

Maybe can I capture the mouse position before the contextmenu gets opened and find the tree node in some ways?

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
    public class Form1 : Form
    {
        private IContainer components;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem aToolStripMenuItem;
        private Telerik.WinControls.UI.RadTreeView radTreeView1;
 
        public Form1()
        {
            this.components = new System.ComponentModel.Container();
            this.radTreeView1 = new Telerik.WinControls.UI.RadTreeView();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.aToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
 
            ((System.ComponentModel.ISupportInitialize) (this.radTreeView1)).BeginInit();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            
            this.radTreeView1.Location = new System.Drawing.Point(16, 12);
            this.radTreeView1.Size = new System.Drawing.Size(251, 152);
            this.radTreeView1.ContextMenuOpening += new Telerik.WinControls.UI.TreeViewContextMenuOpeningEventHandler(this.radTreeView1_ContextMenuOpening);
             
            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aToolStripMenuItem});
            this.contextMenuStrip1.Name = "contextMenuStrip1";
             
            this.aToolStripMenuItem.Name = "aToolStripMenuItem";
            this.aToolStripMenuItem.Text = "A";
            this.aToolStripMenuItem.Click += new System.EventHandler(this.aToolStripMenuItem_Click);
            //
            // Form1
            //
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.radTreeView1);
            ((System.ComponentModel.ISupportInitialize) (this.radTreeView1)).EndInit();
            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);
 
            radTreeView1.ContextMenuStrip = this.contextMenuStrip1;
        }
 
 
        string context = "radTreeView1_ContextMenuOpening WAS NOT executed";
        private void radTreeView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.TreeViewContextMenuOpeningEventArgs e)
        {
            context = "radTreeView1_ContextMenuOpening WAS executed";
        }
         
        private void aToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this, context);
        }
    }
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 30 Nov 2016, 12:48 PM
Hi Andrea,

Thank you for writing.

Since you are using the System.Windows.Forms.ContextMenuStrip class you need to handle its Opening event. Please see my code snippet below: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        this.radTreeView1.ContextMenuStrip = this.contextMenuStrip1;
        this.radTreeView1.ContextMenuStrip.Opening += ContextMenuStrip_Opening;
    }
 
    private void ContextMenuStrip_Opening(object sender, CancelEventArgs e)
    {
        //...
    }
}

The ContextMenuOpening event is defined in the RadTreeView class and if want to handle it you need to use a RadContextMenu. Please check the following documentation article: http://docs.telerik.com/devtools/winforms/treeview/context-menus/default-context-menu.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik by Progress
Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
0
Andrea
Top achievements
Rank 1
answered on 30 Nov 2016, 01:14 PM

Thanks a lot

For future memory the complete snippet:

RadTreeNode contextNode = null;
 
private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
{           
   Point loc = Cursor.Position;           
   RadTreeView t = ((ContextMenuStrip)sender).SourceControl as RadTreeView);
   contextNode = t?.GetNodeAt(t.PointToClient(loc));

 

   //customize context menu, enable disable items etc ..
}

 

Best Regards

Andrea

0
Hristo
Telerik team
answered on 30 Nov 2016, 03:20 PM
Hi Andrea,

Thank you for sharing your code with the community. I am glad that it is working well in your project.

Please let me know if you need further assistance.

Regards,
Hristo Merdjanov
Telerik by Progress
Telerik UI for WinForms is ready for Visual Studio 2017 RC! Learn more.
0
Christ
Top achievements
Rank 2
answered on 11 Sep 2017, 01:42 PM

Thank you both, this was what I was searching for today...

 

/Christ

Tags
Treeview
Asked by
Andrea
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Andrea
Top achievements
Rank 1
Christ
Top achievements
Rank 2
Share this question
or