Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Create a NavBar Control in Code

  • 2 minutes to read

This example creates a NavBarControl in code and adds a Local group with three commands (“Inbox”, “Sent Items”, and “Spam”) to the NavBarControl.

  1. Create a NavBarControl instance.
  2. Create a NavBarGroup instance and add it to the NavBarControl.Groups collection.
  3. For commands, create NavBarItem objects. Use the ImageOptions property to specify an image for each command. Add the commands to the NavBarGroup.ItemLinks collection.

    Tip

    You can use NavBarControl.BeginUpdate and NavBarControl.EndUpdate methods to prevent excessive updates when multiple properties are modified.

  4. Handle the NavBarControl.LinkClicked event to respond to clicks on commands.

WinForms - Create a NavBarControl in Code, DevExpress

Note

svgImageCollection1 was created and populated at runtime.

using DevExpress.XtraNavBar;

void Form1_Load(object sender, EventArgs e) {
  // Create a NavBarControl.
  NavBarControl navBar = new NavBarControl();
  this.Controls.Add(navBar);
  navBar.Dock = DockStyle.Fill;
  // Apply the "SkinExplorerBarView" style.
  navBar.PaintStyleName = "SkinExplorerBarView";
  // Create a Local group.
  NavBarGroup groupLocal = new NavBarGroup("Local");
  // Create an Inbox item and assign an image.
  NavBarItem itemInbox = new NavBarItem("Inbox");
  itemInbox.ImageOptions.SvgImage = svgImageCollection1["inbox"];
  // Create a disabled Sent Items item.
  NavBarItem itemSentItems = new NavBarItem("Sent Items");
  itemSentItems.ImageOptions.SvgImage = svgImageCollection1["sent-items"];
  itemSentItems.Enabled = false;
  // Create a Spam item.
  NavBarItem itemSpam = new NavBarItem("Spam");
  itemSpam.ImageOptions.SvgImage = svgImageCollection1["spam"];
  // Add the items to the group and add the group to the NavBarControl.
  // Prevent excessive updates using BeginUpdate and EndUpdate methods.
  navBar.BeginUpdate();
  navBar.Groups.Add(groupLocal);
  groupLocal.ItemLinks.Add(itemInbox);
  groupLocal.ItemLinks.Add(itemSentItems);
  groupLocal.ItemLinks.Add(itemSpam);
  groupLocal.Expanded = true;
  navBar.EndUpdate();
  // Handle the NavBarControl's LinkClicked event.
  navBar.LinkClicked += new NavBarLinkEventHandler(navBar_LinkClicked);
}

void navBar_LinkClicked(object sender, NavBarLinkEventArgs e) {
  MessageBox.Show(string.Format("The {0} link has been clicked", e.Link.Caption));
}