Search Menu
- 4 minutes to read
The ribbon can display a search box that allows users to search for a bar item by its caption or tags.
To display the search box, set the RibbonControl.ShowSearchItem property to true
. For a Ribbon Form, use the SearchItemPosition property to display the Search box at the top of the form, or in a page header, or to hide it from the Ribbon UI.
Note
Skinnable forms (XtraForm
) cannot display the Search box at the top of the form. Setting the RibbonControl.SearchItemPosition
to SearchItemPosition.Caption
hides the search box.
The SearchItemShortcut property allows you to specify the shortcut used to focus the search box.
using DevExpress.XtraBars;
ribbonControl1.ShowSearchItem = true;
ribbonControl1.OptionsSearchMenu.SearchItemShortcut = new BarShortcut((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F));
Note
In an MDI application, a child form’s search box is not added to the parent form when ribbons are merged. You should enable the search box on the parent and child forms individually.
Bar Items in the Menu
The search menu finds a bar item by its Caption. Use the SearchTags property to provide custom keywords that can be used to locate the bar item.
using DevExpress.XtraBars;
BarButtonItem closeButton = new BarButtonItem();
closeButton.Caption = "Close";
closeButton.SearchTags = "Quit, Exit";
closeButton.ItemClick += CloseButton_ItemClick;
ribbonControl1.Items.Add(closeButton);
ribbonPageGroup1.ItemLinks.Add(closeButton);
private void CloseButton_ItemClick(object sender, ItemClickEventArgs e) {
this.Close();
}
To hide a bar item in the search menu, set its VisibleInSearchMenu property to false
.
Customize the Menu
The CustomizeSearchMenu event fires when the query in the search box changes and allows you to customize the menu. The following properties provide information specific to this event:
- SearchString – Gets the search query.
- Menu – Gets the search menu (see PopupMenu). The
Menu.ItemLinks
collection contains all the available bar item links. An item link is visible or hidden based on the current search query. - AddHeader(String) – Adds a header to the search menu.
- AddItem(BarItem) – Adds an item to the search menu.
- ShowNoMatchesItem – Specifies whether to display the No matches found item.
Enable the RibbonOptionsSearchMenu.UseCustomRibbonSearch option to populate the search menu only through the CustomizeSearchMenu
event.
The code below shows how to add a custom button to the menu and show an item regardless of the current query.
using DevExpress.XtraBars;
ribbonControl1.CustomizeSearchMenu += RibbonControl1_CustomizeSearchMenu;
BarButtonItem shareButton;
BarButtonItem ShareButton {
get {
if (shareButton == null) {
BarButtonItem shareButton = new BarButtonItem();
shareButton.Caption = "Share";
shareButton.ItemClick += (s, e) => MessageBox.Show("Share");
shareButton.Manager = ribbonControl1.Manager;
this.shareButton = shareButton;
}
return shareButton;
}
}
private void ribbonControl1_CustomizeSearchMenu(object sender, DevExpress.XtraBars.Ribbon.RibbonSearchMenuEventArgs e) {
// Add a custom button that is always visible.
e.AddItem(ShareButton);
// Show the Online Help command regardless of the current query.
e.Menu.ItemLinks.Where(x => x.Item == biOnlineHelp).First().Visible = true;
}
Enter Key Behavior
The RibbonControl.SearchMenuEnterPressed event allows you to specify actions executed when a user presses Enter in the search box.
The following code snippet saves the user query, executes the first found item, and closes the Search Menu on the Enter key press:
List<string> mruStrings = new List<string>();
void ribbonControl1_SearchMenuEnterPressed(object sender, RibbonSearchMenuEnterPressedEventArgs e) {
if (!mruStrings.Contains(e.SearchString))
mruStrings.Insert(0, e.SearchString);
e.ExecuteFirstSearchResult();
e.HideSearchMenu();
}
Localize the Menu
Use the Localization Service to add culture-specific messages to the search menu. The code sample below shows how to localize the message shown when the menu has no search query and the message displayed when no matches are found.
using DevExpress.XtraBars.Localization;
BarLocalizer.Active = new CustomBarLocalizer();
public class CustomBarLocalizer : BarLocalizer {
public override string GetLocalizedString(BarString id) {
if (id == BarString.RibbonSearchItemNullText)
return "Suche";
if (id == BarString.RibbonSearchItemNoMatchesFound)
return "Keine Treffer gefunden";
return base.GetLocalizedString(id);
}
}