Cant find contextmenuopening event for Listview

1 Answer 550 Views
ContextMenu ListView
Shuvrangshu
Top achievements
Rank 1
Shuvrangshu asked on 28 Jun 2021, 04:45 AM
I have to add a context menu to the list view but need the menu items to change depending on the items clicked, but am not able to find the contextmenuopening event for the listbox. am i missing something or is there a different way to handle this.
I have used this same method for treeview and that works perfectly.

1 Answer, 1 is accepted

Sort by
2
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 28 Jun 2021, 10:43 AM

Hello, Shuvrangshu,

RadListView control doesn't offer the ContectMenuOpening event. In RadListView you can show a RadContextMenu when the mouse right button is pressed by using the MouseDown event or use the ItemMouseDown event when the user presses a mouse button over ListViewDataItem. More information is available here:
https://docs.telerik.com/devtools/winforms/controls/menus/contextmenu/context-menus 
https://docs.telerik.com/devtools/winforms/controls/menus/contextmenu/add-context-menu-in-code 

RadContextMenu offers a DropDownOpening event which occurs before the pop-up is shown and you have access to the items that are about to be shown.

I prepared an example for your reference where a context menu with one item "Delete" is shown when the user clicks with the right mouse button over the first data item in the list:

RadContextMenu contextMenu;
public RadForm1()
{
    InitializeComponent();

    contextMenu = new RadContextMenu();
    RadMenuItem menuItem = new RadMenuItem("Delete");
    contextMenu.Items.Add(menuItem);

    this.radListView1.ItemMouseDown += RadListView1_ItemMouseDown;
}

private void RadListView1_ItemMouseDown(object sender, ListViewItemMouseEventArgs e)
{
    if (e.OriginalEventArgs.Button == System.Windows.Forms.MouseButtons.Right && e.Item.Text == "ListViewItem 1")
    {
        contextMenu.Show(radListView1, e.OriginalEventArgs.X, e.OriginalEventArgs.Y);
    }
}

Feel free to extend this example to display different items depending on the item clicked.

I hope this helps. Should you have other questions do not hesitate to ask.

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Shuvrangshu
Top achievements
Rank 1
commented on 29 Jun 2021, 03:23 PM

This really helped me. thanks for this.
Tags
ContextMenu ListView
Asked by
Shuvrangshu
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or