Introduction
Elegant Ribbon provides full support for context menus. You can associate a context menu with one or more instances of any System.Windows.Forms.Control-derived class so that when the user right-clicks the control or presses the Context Menu key for the selected control, the context menu appears on the screen (see Figure 1). You can also show a context menu at any point of the screen without associating it to a control.
Figure 1 Context menu over a panel control on the form
As with other Elegant Ribbon controls, you can do most of the work using the Windows Forms Designer.
Adding and Populating a Context Menu
In the Toolbox under the Elegant Ribbon tab, find the ContextMenu control and drag it onto the form. You should see an icon representing the created context menu in the bottom part of the Windows Forms Designer. Now if you select the icon, you should see a context menu at the top of the Windows Forms Designer (see Figure 2). The context menu contains controls which you can add to the context menu control. So you can populate the context menu control in same way as you do for common pop-up menus.
Figure 2 Adding items to the context menu
Associating Context Menus with Controls
After you created at least one instance of ContextMenu, you can associate it with one or more System.Windows.Forms.Control-derived controls on the form. You should set the ContextPopupMenu property of a control to the name of a context menu (see Figure 3). The reason why the property is called ContextPopupMenu rather than ContextMenu is that the standard System.Windows.Forms.Control class already contains such a property.
Figure 3 Associating a context menu with a form
Please note the ContextPopupMenu property is available at design time only. If you want to associate context menus with controls programmatically, you can do that using the ContextMenu.RegisterControlContextMenu() static method, which takes two arguments: a context menu and a control that should be associated with the context menu. To remove the connection between a context menu and a control, use the ContextMenu.UnregisterControlContextMenu() static method.
Advanced Tasks
Context Menu Events
The ContextMenu class has a set of static events that allow you to handle context menu-related events in the application scope. These events include:
- ContextMenu.ContextMenuShowing
- ContextMenu.ContextMenuHidingAnimationStopped
- ContextMenu.ContextMenuVisibleChanged
- ContextMenu.ContextMenuRequired
The ContextMenu.ContextMenuRequired event enables you to resolve associations between context menus and controls in code. It occurs when the framework fails to find any context menu associated with a control. In the event handler, you can assign a context menu for any control on the fly.
Active Context Menu
The following table contains the method and properties related to the active context menu, i.e. the context menu that is currently displayed on the screen.
Method/Property of ContextMenu | Description |
HideContextMenu() method | Hides the active context menu. | ShownContextMenu property | Gets the active context menu. | IsContextMenuShown property | Checks if a context menu is active. |
Context Menu at an Arbitrary Location
You can show a context menu manually at any location of the screen using the ContextMenu.ShowContextMenu() method.
|