TreeView event ContextMenuOpening not executing with ContextMenuStrip

5 posts, 1 answers
  1. Andrea
    Andrea avatar
    61 posts
    Member since:
    Oct 2012

    Posted 30 Nov 2016 Link to this post

    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);
            }
        }
    }
  2. Answer
    Hristo
    Admin
    Hristo avatar
    1018 posts

    Posted 30 Nov 2016 Link to this post

    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.
  3. Andrea
    Andrea avatar
    61 posts
    Member since:
    Oct 2012

    Posted 30 Nov 2016 in reply to Hristo Link to this post

    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

  4. Hristo
    Admin
    Hristo avatar
    1018 posts

    Posted 30 Nov 2016 Link to this post

    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.
  5. Christ
    Christ avatar
    9 posts
    Member since:
    Feb 2011

    Posted 11 Sep Link to this post

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

     

    /Christ

Back to Top