WinForms ContextMenu Overview
To implement context menus use RadContextMenu in your application. RadContextMenu is a non-visual component that sits in the component tray located below the form design surface. RadContextMenu, like RadMenu, can be themed and has Items collection that accepts RadMenuItem, RadMenuComboBoxItem, RadMenuSeparatorItem and RadMenuContextItem. You can subscribe to the Click event of each menu item and execute the desired logic.
The ContextMenu is part of Telerik UI for WinForms, a
professional grade UI library with 160+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.
Figure 1: RadContextMenu
There are two ways to attach a context menu to a given control or portion of a control:
- For those RadControls that have a ContextMenu or RadContextMenu properties you can assign the RadContextMenu in the designer or in code.
Assigning a RadContextMenu
radTreeView1.Nodes[0].ContextMenu = radContextMenu1;
RadTreeView1.Nodes(0).ContextMenu = RadContextMenu1
The ContextMenuStrip property refers to a Windows standard control. This property drop down will not display RadMenu or RadContextMenu components that exist on the form.
- Handle the mouse down event for the control that requires the context menu and call the RadContextMenu.Show() method.
Handling the MouseDown event
void radCalendar1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point p = (sender as Control).PointToScreen(e.Location);
radContextMenu1.Show(p.X, p.Y);
}
}
Private Sub radCalendar1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Right Then
Dim p As Point = (TryCast(sender, Control)).PointToScreen(e.Location)
RadContextMenu1.Show(p.X, p.Y)
End If
End Sub