Skip to main content

DropDownButton Class

The button that can be associated with a popup control or a context menu. It is possible to prevent the button from receiving focus on a click.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.1.dll

NuGet Package: DevExpress.Win.Navigation

Declaration

public class DropDownButton :
    SimpleButton,
    IDXMenuSupport,
    IDXDropDownControlOwner

Remarks

Use this control if a button needs to be associated with a popup control or a context menu. The DropDownButton object can contain a dropdown arrow, which can either be displayed as a separate button, merged into the main button, or hidden:

DropDownButton

Use the DropDownButton.DropDownArrowStyle property to specify the display mode of the dropdown arrow.

To associate the button with a popup control/context menu, use the DropDownButton.DropDownControl property. The following objects can be used as popup controls:

Note

If you use a PopupMenu or PopupControlContainer as a dropdown control, a MenuManager must be specified. See DropDownButton.MenuManager to learn more.

Setting the DropDownButton.ActAsDropDown property to false prevents the associated DropDownButton.DropDownControl from being displayed.

The SimpleButton.AllowFocus inherited property can be set to false to prevent the button from being focused.

Example

The following example creates a DropDownButton and associates it with a PopupMenu. This PopupMenu is invoked when you click the button’s dropdown arrow.

DropdownButton.png

using DevExpress.XtraBars;
using DevExpress.XtraEditors;

namespace WindowsFormsApplication1 {

    public partial class Form1 : Form {

        DropDownButton dropDownButton1;
        BarManager barManager1;
        PopupMenu popupMenu1;
        BarButtonItem btnZoomIn;
        BarButtonItem btnZoomOut;

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            InitControls();
            UpdateDropDownButton(btnZoomIn);
            dropDownButton1.Appearance.Font = WindowsFormsSettings.DefaultMenuFont;
        }

        private void InitControls() {
            barManager1 = new BarManager();
            barManager1.Form = this;

            dropDownButton1 = new DropDownButton();
            popupMenu1 = new PopupMenu(barManager1);
            btnZoomIn = new BarButtonItem(barManager1, "Zoom In");
            btnZoomOut = new BarButtonItem(barManager1, "Zoom Out");
            popupMenu1.AddItem(btnZoomIn);
            popupMenu1.AddItem(btnZoomOut);

            // 
            // dropDownButton1
            // 
            dropDownButton1.Parent = this;
            dropDownButton1.Location = new System.Drawing.Point(127, 89);
            dropDownButton1.Size = new System.Drawing.Size(152, 29);
            dropDownButton1.DropDownControl = popupMenu1;
            dropDownButton1.ImageOptions.ImageToTextAlignment = DevExpress.XtraEditors.ImageAlignToText.LeftCenter;
            dropDownButton1.ImageOptions.ImageToTextIndent = 10;
            dropDownButton1.Click += new System.EventHandler(this.dropDownButton1_Click);

            // 
            // btnZoomIn
            // 
            btnZoomIn.ImageOptions.SvgImage = global::WindowsFormsApplication1.Properties.Resources.zoomin;
            btnZoomIn.Tag = "zoomin";
            btnZoomIn.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.btnZoomIn_ItemClick);
            // 
            // btnZoomOut
            // 
            btnZoomOut.ImageOptions.SvgImage = global::WindowsFormsApplication1.Properties.Resources.zoomout;
            btnZoomOut.Tag = "zoomout";
            btnZoomOut.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.btnZoomOut_ItemClick);
        }

        private void btnZoomIn_ItemClick(object sender, ItemClickEventArgs e) {
            UpdateDropDownButton(e.Item);
            //...
            ZoomIn();
        }

        private void btnZoomOut_ItemClick(object sender, ItemClickEventArgs e) {
            UpdateDropDownButton(e.Item);
            //...
            ZoomOut();
        }

        private void UpdateDropDownButton(BarItem submenuItem) {
            dropDownButton1.Text = submenuItem.Caption;
            dropDownButton1.ImageOptions.SvgImage = submenuItem.ImageOptions.SvgImage;
            dropDownButton1.ImageOptions.SvgImageSize = new Size(16, 16);
            dropDownButton1.Tag = submenuItem.Tag;
        }

        private void dropDownButton1_Click(object sender, EventArgs e) {
            string tag = (sender as DropDownButton).Tag.ToString();
            if (tag == "zoomin") {
                ZoomIn();
            }
            if(tag == "zoomout") {
                ZoomOut();
            }
        }

        void ZoomIn() {
            MessageBox.Show("Zoom In");
        }

        void ZoomOut() {
            MessageBox.Show("Zoom Out");
        }
    }
}

Inheritance

See Also