New to Telerik UI for WinForms? Download free 30-day trial

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";
RadMenuItem menuItem2 = new RadMenuItem();
menuItem2.Text = "Hide";

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

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:

WinForms RadContextMenu Apply Sample

In this article