Back to Devexpress

DropDownButton Class

windowsforms-devexpress-dot-xtraeditors-7ad61c99.md

latest10.6 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXLicenseWinFormsEditors]
public class DropDownButton :
    SimpleButton,
    IDXMenuSupport,
    IDXDropDownControlOwner
vb
<DXLicenseWinFormsEditors>
Public Class DropDownButton
    Inherits SimpleButton
    Implements 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:

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.

View Example

csharp
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");
        }
    }
}
vb
Imports DevExpress.XtraBars
Imports DevExpress.XtraEditors

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        InitControls()
        UpdateDropDownButton(btnZoomIn)
        dropDownButton1.Appearance.Font = WindowsFormsSettings.DefaultMenuFont
    End Sub

    Private dropDownButton1 As DropDownButton
    Private barManager1 As BarManager
    Private popupMenu1 As PopupMenu
    Private btnZoomIn As BarButtonItem
    Private btnZoomOut As BarButtonItem

    Private Sub InitControls()
        BarManager1 = New BarManager()
        BarManager1.Form = Me
        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.Parent = Me
        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
        AddHandler dropDownButton1.Click, New System.EventHandler(AddressOf Me.dropDownButton1_Click)
        btnZoomIn.ImageOptions.SvgImage = Global.WindowsApplication1.My.Resources.Resources.zoomin
        btnZoomIn.Tag = "zoomin"
        AddHandler btnZoomIn.ItemClick, AddressOf Me.btnZoomIn_ItemClick
        btnZoomOut.ImageOptions.SvgImage = Global.WindowsApplication1.My.Resources.Resources.zoomout
        btnZoomOut.Tag = "zoomout"
        AddHandler btnZoomOut.ItemClick, AddressOf Me.btnZoomOut_ItemClick
    End Sub

    Private Sub btnZoomIn_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
        UpdateDropDownButton(e.Item)
        ZoomIn()
    End Sub

    Private Sub btnZoomOut_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
        UpdateDropDownButton(e.Item)
        ZoomOut()
    End Sub

    Private Sub UpdateDropDownButton(ByVal submenuItem As BarItem)
        dropDownButton1.Text = submenuItem.Caption
        dropDownButton1.ImageOptions.SvgImage = submenuItem.ImageOptions.SvgImage
        dropDownButton1.ImageOptions.SvgImageSize = New Size(16, 16)
        dropDownButton1.Tag = submenuItem.Tag
    End Sub

    Private Sub dropDownButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim tag As String = (TryCast(sender, DropDownButton)).Tag.ToString()

        If tag = "zoomin" Then
            ZoomIn()
        End If

        If tag = "zoomout" Then
            ZoomOut()
        End If
    End Sub

    Private Sub ZoomIn()
        MessageBox.Show("Zoom In")
    End Sub

    Private Sub ZoomOut()
        MessageBox.Show("Zoom Out")
    End Sub

End Class

Implements

IXtraResizableControl

Inheritance

Show 11 items

Object MarshalByRefObject Component Control DevExpress.XtraEditors.XtraControl ControlBase BaseControl BaseStyleControl BaseButton SimpleButton DropDownButton

See Also

DropDownButton Members

DXPopupMenu

PopupControlContainer

PopupMenu

DevExpress.XtraEditors Namespace