Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Menu, Application Menu, Context Menu > RadTreeView and RadContextMenu

Not answered RadTreeView and RadContextMenu

Feed from this thread
  • Adrian avatar

    Posted on Jun 22, 2011 (permalink)

    I have a RadTreeView with top nodes and each top node may have children (subnotes) depending on my database data source.

    Top node 1
        |___ Sub node 1
        |___ Sub node 2
        |___ Sub node 3
    Top node 2
        |___ Sub node 1
        |___ Sub node 2
        |___ Sub node 3
        |___ Sub node 4
    Top node 3
        |___ Sub node 1

    I have consigured all my top nodes to have a ContextMenu (radContextMenu1) associated with it.
    For all the Sub notes of any top node (so for Sub node 1, 2, 3 of Top node 1; Sub node 1, 2, 3, 4 of Top node 2 and Sub node 1 of Top node 3 and any other subsequent Sub note of any other possible Top node), I want a context menu (radContextMenu2) to appear and I want to do this through C# code, not at design time.
    The only example that you seem to provide is how to acomplish this at design time and only with predefined nodes; not database driven ones, like mine.

    Here is the only example that you provide:

    RadTreeView1.Nodes[0].ContextMenu = radContextMenu2;

    This will NOT work in my case since my menu is constantly changing based on my dasaset.
    In essence, I am looking for something like this:

    RadTreeView1.AllSubNodesAtLevel[0].ContextMenu = radContextMenu2;
    (where level 0 would be ALL the Sub nodes in the above pictured schema)

    Thank you!

    Reply

  • Nikolay Nikolay admin's avatar

    Posted on Jun 22, 2011 (permalink)

    Hi Adrian,

    Thank you for your question.

    In order to show a custom menu at specified nodes, you can plug in the context menu implementation of RadTreeView. Shortly said, from the event arguments of the ContextMenuOpening event you can take the node at which a context menu should be shown, cancel the default menu and show you own menu:
    public partial class Form1 : Form
    {
        RadContextMenu menu = new RadContextMenu();
      
        public Form1()
        {
            InitializeComponent();
      
            RadMenuItem item1 = new RadMenuItem("MenuItem1");
            RadMenuItem item2 = new RadMenuItem("MenuItem2");
            RadMenuItem item3 = new RadMenuItem("MenuItem3");
            menu.Items.Add(item1);
            menu.Items.Add(item2);
            menu.Items.Add(item3);
            item1.Click += new EventHandler(item1_Click);
              
      
            this.radTreeView1.AllowDefaultContextMenu = true;
      
            this.radTreeView1.ContextMenuOpening += new Telerik.WinControls.UI.TreeViewContextMenuOpeningEventHandler(radTreeView1_ContextMenuOpening);
        }
      
        void item1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("MenuItem1 says: I was clicked");
        }
      
        void radTreeView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.TreeViewContextMenuOpeningEventArgs e)
        {
            if (e.Node.Level == 1)
            {
                e.Cancel = true;
                menu.Show(Cursor.Position);
            }            
        }
    }

    I hope this helps.

    All the best,
    Nikolay
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Menu, Application Menu, Context Menu > RadTreeView and RadContextMenu
Related resources for "RadTreeView and RadContextMenu"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]