Back to Devexpress

How to: Add Items to Bars' Customization Menu

windowsforms-5423-controls-and-libraries-ribbon-bars-and-menu-examples-bars-how-to-add-items-to-bars-customization-menu.md

latest3.8 KB
Original Source

How to: Add Items to Bars' Customization Menu

  • Aug 01, 2019
  • 3 minutes to read

The following example shows how you can handle the BarManager.ShowToolbarsContextMenu event to manipulate the customization menu. In this example, a new bar item link representing the hyperlink editor is added to the menu. This editor displays “www.devexpress.com” as text, clicking on this opens a browser window at the specified address.

The bar item providing the editing facilities is a BarEditItem. To associate a specific editor with the bar item, we create a corresponding repository item object which contains edit type information and edit settings and add it to the BarManager’s ComponentEditorContainer.RepositoryItems collection, then assign it to the BarEditItem.Edit property. The bar item is created and customized in the form’s Load event handler.

The image below shows the customization menu with the new item:

csharp
using XtraEditors.Repository;
private void frmMain_Load(object sender, System.EventArgs e) {
    //Create a new bar item representing a hyperlink editor
    BarEditItem item = new BarEditItem();
    //Create and customize a repository item representing a hyperlink editor
    RepositoryItemHyperLinkEdit ri = new RepositoryItemHyperLinkEdit();
    ri.SingleClick = true;
    //Add the repository item to the internal repository
    barManager1.RepositoryItems.Add(ri);
    //Assign the repository item to the bar item
    item.Edit = ri;
    //Provide the initial value for the editor
    item.EditValue = "www.devexpress.com";
    //Name the bar item
    item.Name = "MyHyperlinkItem";
    //Add the bar item to the bar manager
    barManager1.Items.Add(item);
}

private void barManager1_ShowToolbarsContextMenu(object sender, 
  ShowToolbarsContextMenuEventArgs e) {
    BarManager barManager = sender as BarManager;
    //Get the bar item by its name and create a link to it in the customization menu
    BarItemLink link = e.ItemLinks.Add(barManager.Items["MyHyperlinkItem"]);
    //Customize the link
    link.Width = 120;
    link.BeginGroup = true;
}
vb
Imports XtraEditors.Repository

Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
 Handles MyBase.Load
    'Create a new bar item representing a hyperlink editor
    Dim item As BarEditItem = New BarEditItem
    'Create and customize a repository item representing a hyperlink editor
    Dim ri As RepositoryItemHyperLinkEdit = New RepositoryItemHyperLinkEdit
    ri.SingleClick = True
    'Add the repository item to the internal repository
    barManager1.RepositoryItems.Add(ri)
    'Assign the repository item to the bar item
    item.Edit = ri
    'Provide the initial value for the editor
    item.EditValue = "www.devexpress.com"
    'Name the bar item 
    item.Name = "MyHyperlinkItem"
    'Add the bar item to the bar manager
    barManager1.Items.Add(item)
End Sub

Private Sub barManager1_ShowToolbarsContextMenu(ByVal sender As Object, _
 ByVal e As DevExpress.XtraBars.ShowToolbarsContextMenuEventArgs) _
 Handles barManager1.ShowToolbarsContextMenu
    Dim barManager as BarManager = sender
    'Get the bar item by its name and create a link to it in the customization menu
    Dim link As BarItemLink = e.ItemLinks.Add(barManager.Items("MyHyperlinkItem"))
    'Customize the link
    link.Width = 120
    link.BeginGroup = True
End Sub