Showing Popup Menus

  • 4 minutes to read

Popup menus can be used in a number of ways. They can be displayed automatically as a result of end-user's actions, or you can implement your own logic and show popup menus manually. See below to learn more.

NOTE

Before customizing and using a popup menu, add a BarManager or RibbonControl to the form and ensure that the popup menu is bound to this BarManager/RibbonControl. By default, a popup menu being added to the form at design time automatically looks for a BarManager/Ribbon Control within the form, and if it's found, binds itself to the located object via the PopupMenuBase.Manager/PopupMenuBase.Ribbon property. When creating the menu at runtime, you may need to manually bind it to a BarManager/Ribbon Control via these properties.

Automatic Display for Controls

It's possible to associate a popup menu with a control and automatically display this menu when an end-user right-clicks the control. To assign a menu to a control, use the control's external PopupContextMenu property. The external PopupContextMenu properties are added at design time for all controls within a form when you place a BarManager or RibbonControl component onto this form.

Add a popup menu to the form. Then, select a control and switch to the Properties window. Locate the external PopupContextMenu property and assign the popup menu to this property.

Bars3_ExternalContextMenu

The menu assigned will be displayed at runtime when right-clicking within the control's bounds.

Note that the external PopupContextMenu property is available only at design time. To associate a menu with a control via code, you can use the BarManager.SetPopupContextMenu method. Call BarManager.GetPopupContextMenu to retrieve the control's popup menu.

Example

This example creates a PopupMenu with three items and binds this menu to the form. You can right-click the form at runtime to invoke the menu.

using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PopupMenu {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // Finish BarManager initialization (to allow its further customization on the form load)
            barManager1.ForceInitialize();

            DevExpress.XtraBars.PopupMenu menu = new DevExpress.XtraBars.PopupMenu();
            menu.Manager = barManager1;
            barManager1.Images = imageCollection1;
            BarButtonItem itemCopy = new BarButtonItem(barManager1, "Copy", 0);
            BarButtonItem itemPaste = new BarButtonItem(barManager1, "Paste", 1);
            BarButtonItem itemRefresh = new BarButtonItem(barManager1, "Refresh", 2);
            menu.AddItems(new BarItem[] { itemCopy, itemPaste, itemRefresh });
            // Create a separator before the Refresh item.
            itemRefresh.Links[0].BeginGroup = true;
            // Process item clicks.
            barManager1.ItemClick += BarManager1_ItemClick;
            // Associate the popup menu with the form.
            barManager1.SetPopupContextMenu(this, menu);
        }

        private void BarManager1_ItemClick(object sender, ItemClickEventArgs e) {
            XtraMessageBox.Show(e.Item.Caption + " item clicked");
        }
    }
}

Manual Display

To activate popup menus, you normally handle specific events. For instance, if you need to open a popup menu when an end-user right-clicks within a text box, you may want to handle the control's MouseUp event. To open a menu, call PopupMenu.ShowPopup.

Example

The following code displays a specific PopupMenu if the right mouse button is pressed:

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
    if(e.Button == MouseButtons.Right)
        popupMenu1.ShowPopup(Control.MousePosition);
}

Automatic Display for Bar Button Items

You can associate a dropdown control (including a PopupMenu object) with regular and large bar button items (BarButtonItem or BarLargeButtonItem). In this instance, this item displays a dropdown arrow and clicking this arrow shows the associated dropdown control below the item:

Bars3_ActAsDropDown_true

To associate a popup menu with a bar button item, use the BarButtonItem.DropDownControl property. In addition, the bar item's BarButtonItem.ButtonStyle property must be set to BarButtonStyle.DropDown.