Add Context menu in the code

This article demonstrates how you can add RadContextMenu in the code and attach it to a specific control.

1. Adding a RadContextMenu at runtime. To programmatically add a RadContextMenu to a form, create a new instance of a RadContextMenu

RadContextMenu radContextmenu = new RadContextMenu();

Private radContextmenu As RadContextMenu = New RadContextMenu()

2. Add RadMenuItems to RadContextMenu.

RadMenuItem menuItem1 = new RadMenuItem();
menuItem1.Text = "Show";
radContextmenu.Items.Add(menuItem1);
RadMenuItem menuItem2 = new RadMenuItem();
menuItem2.Text = "Hide";
radContextmenu.Items.Add(menuItem2);

Dim menuItem1 As RadMenuItem = New RadMenuItem()
menuItem1.Text = "Show"
radContextmenu.Items.Add(menuItem1)
Dim menuItem2 As RadMenuItem = New RadMenuItem()
menuItem2.Text = "Hide"
radContextmenu.Items.Add(menuItem2)

3. Subscribe to đťouseClick event of the control and call the RadContextMenu.Show() method:

private void RadListControl1_Click(object sender, EventArgs e)
{
    var args = e as MouseEventArgs;
    if (args.Button == MouseButtons.Right)
    {
        radContextmenu.Show(radListControl1, args.Location);
    }
}

Private Sub RadListControl1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim args = TryCast(e, MouseEventArgs)
    If args.Button = MouseButtons.Right Then
        radContextmenu.Show(radListControl1, args.Location)
    End If
End Sub

4. Here is the result:

menus-context-menu-add-context-meni-in-code

In this article
Not finding the help you need? Improve this article