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

Expand/open a context menu's sub menu item programmatically

2 Answers 107 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Arash
Top achievements
Rank 1
Veteran
Arash asked on 17 Feb 2021, 04:20 PM

Hi,

Is it possible to open a context menu's sub menu item programmatically?

I have a context menu with sub menu items, as shown in the attached image.

I know how to show the context menu in a particular location but I can't find a way of also opening/expanding sub menu "Item 2" so that menu items "Sub item 1" and "Sub item 2" are shown to the user.

Not sure if it make any difference but, the context menu is activated by right click on a RadGridView. 

Thanks,

Arash


2 Answers, 1 is accepted

Sort by
0
Nadya
Telerik team
answered on 19 Feb 2021, 01:37 PM

Hello, Arash,

Yes, it is possible to show the menu items as well as the sub menu items programmatically. RadMenuItem has ShowChildItems method that shows the sub items. You can use the following code snippet:

public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    private RadContextMenu contextMenu;
    public RadForm1()
    {
        InitializeComponent();
        contextMenu = new RadContextMenu();
        RadMenuItem menuItem1 = new RadMenuItem("Item 1");
        RadMenuItem menuItem2 = new RadMenuItem("Item 2");
        RadMenuItem menuItem3 = new RadMenuItem("Sub menu item 1");
        RadMenuItem menuItem4 = new RadMenuItem("Sub menu item 2");
        menuItem2.Items.Add(menuItem3);
        menuItem2.Items.Add(menuItem4);
        contextMenu.Items.Add(menuItem1);
        contextMenu.Items.Add(menuItem2);
        this.radGridView1.ContextMenuOpening += this.RadGridView1_ContextMenuOpening;
        contextMenu.DropDownOpened += this.ContextMenu_DropDownOpened;
    }

    private void ContextMenu_DropDownOpened(object sender, EventArgs e)
    {
        foreach (RadMenuItem item in contextMenu.Items)
        {
            if (item.HasChildItemsToShow)
            {
                item.ShowChildItems();
            }
        }
    }

    private void RadGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        e.ContextMenu = contextMenu.DropDown;
    }

I hope this helps. If you have other questions do not hesitate to contact me.

Regards,
Nadya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Arash
Top achievements
Rank 1
Veteran
answered on 19 Feb 2021, 02:10 PM

Hi Nadya,

 

Thanks, that's exactly what I was looking for.

Regards,
Arash

Tags
ContextMenu
Asked by
Arash
Top achievements
Rank 1
Veteran
Answers by
Nadya
Telerik team
Arash
Top achievements
Rank 1
Veteran
Share this question
or