Back to Devexpress

RibbonControl.ShowCustomizationMenu Event

windowsforms-devexpress-dot-xtrabars-dot-ribbon-dot-ribboncontrol-2d8e42c7.md

latest5.5 KB
Original Source

RibbonControl.ShowCustomizationMenu Event

Occurs before the RibbonControl’s Customization Menu is displayed.

Namespace : DevExpress.XtraBars.Ribbon

Assembly : DevExpress.XtraBars.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event RibbonCustomizationMenuEventHandler ShowCustomizationMenu
vb
<DXCategory("Events")>
Public Event ShowCustomizationMenu As RibbonCustomizationMenuEventHandler

Event Data

The ShowCustomizationMenu event's data class is DevExpress.XtraBars.Ribbon.RibbonCustomizationMenuEventArgs.

Remarks

The Customization Menu is displayed when a Ribbon Control is right-clicked. You can handle the ShowCustomizationMenu event to customize the menu or prevent it from being displayed.

The customization menu is not recreated each time it needs to be displayed. Therefore, if the menu has been modified in the ShowCustomizationMenu event handler, the changes will persist the next time the ShowCustomizationMenu event fires. So, before modifying the menu, you generally need to check whether modifications have been already made or not.

Example

The following example handles the RibbonControl.ShowCustomizationMenu event to customize the RibbonControl’s context menu.

The example hides the built-in “Add to Quick Access Toolbar” command, and adds a custom “About” command to the menu.

csharp
using DevExpress.XtraBars;
using DevExpress.XtraBars.Localization;

private void ribbonControl1_ShowCustomizationMenu(object sender, DevExpress.XtraBars.Ribbon.RibbonCustomizationMenuEventArgs e) {
    // Check if the context menu is invoked after a link is right-clicked.
    if (e.Link == null) return;
    // Locate and hide the "Add to Quick Access Toolbar" command in the context menu
    BarItemLink linkAddToQat = e.CustomizationMenu.ItemLinks.Where(link => link.Caption == BarLocalizer.Active.GetLocalizedString(BarString.RibbonToolbarAdd)).FirstOrDefault();
    linkAddToQat.Visible = false;

    // Check if a custom "About" command has already been created.
    BarItemLink menuAboutCommand = e.CustomizationMenu.ItemLinks.Where(link => link.Caption == "About").FirstOrDefault();
    // Add the "About" command.
    if (menuAboutCommand == null) {
        menuAboutCommand = e.CustomizationMenu.AddItem(GetAboutCommand());
        // Add a separator before the command.
        menuAboutCommand.BeginGroup = true;
    }
}

BarItem biAbout;
BarItem GetAboutCommand() {
    if (biAbout == null) {
        biAbout = new BarButtonItem();
        biAbout.Caption = "About";
        biAbout.ItemClick += new ItemClickEventHandler(biAbout_ItemClick);
        ribbonControl1.Items.Add(biAbout);
    }
    return biAbout;
}
// The method invoked when the "About" command is clicked.
void biAbout_ItemClick(object sender, ItemClickEventArgs e) {
    MessageBox.Show("About");
}
vb
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Localization

Private Sub RibbonControl1_ShowCustomizationMenu(sender As Object, e As DevExpress.XtraBars.Ribbon.RibbonCustomizationMenuEventArgs) Handles RibbonControl1.ShowCustomizationMenu
    ' Check if the context menu is invoked after a link is right-clicked.
    If e.Link Is Nothing Then Return
    ' Locate and hide the "Add to Quick Access Toolbar" command in the context menu
    Dim linkAddToQat As BarItemLink = e.CustomizationMenu.ItemLinks.Where(Function(link) link.Caption = BarLocalizer.Active.GetLocalizedString(BarString.RibbonToolbarAdd)).FirstOrDefault()
    linkAddToQat.Visible = False
    ' Check if a custom "About" command has already been created.
    Dim menuAboutCommand As BarItemLink = e.CustomizationMenu.ItemLinks.Where(Function(link) link.Caption = "About").FirstOrDefault()
    ' Add the "About" command.
    If menuAboutCommand Is Nothing Then
        menuAboutCommand = e.CustomizationMenu.AddItem(GetAboutCommand())
        ' Add a separator before the command.
        menuAboutCommand.BeginGroup = True
    End If
End Sub

Private biAbout As BarItem = Nothing
Private Function GetAboutCommand() As BarItem
    If biAbout Is Nothing Then
        biAbout = New BarButtonItem()
        biAbout.Caption = "About"
        AddHandler biAbout.ItemClick, AddressOf biAbout_ItemClick
        RibbonControl1.Items.Add(biAbout)
    End If
    Return biAbout
End Function

' The method invoked when the "About" command is clicked.
Private Sub biAbout_ItemClick(ByVal sender As System.Object,
  ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
    MessageBox.Show("About")
End Sub

See Also

ShowQatLocationSelector

CustomizeQatMenu

RibbonControl Class

RibbonControl Members

DevExpress.XtraBars.Ribbon Namespace