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

RadTreeView and RadContextMenu

1 Answer 311 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Adrian
Top achievements
Rank 1
Adrian asked on 22 Jun 2011, 03:11 PM
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!

1 Answer, 1 is accepted

Sort by
0
Nikolay
Telerik team
answered on 22 Jun 2011, 05:01 PM
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.
Tags
ContextMenu
Asked by
Adrian
Top achievements
Rank 1
Answers by
Nikolay
Telerik team
Share this question
or