下列代码展示了如何在代码中创建 bars 和 bar items。
在本示例中创建了两个 bars —— 一个主菜单 (一个被拉伸并与窗体宽度匹配的栏) 和一个常规 bar。 主菜单将包含三个子菜单 (File、Edit 和 View),每个子菜单都有自己的 bar items。 第二个 bar 将显示 Output 按钮,该按钮也在 View 子菜单中的第一个 bar item 可用:
Bars 被添加到 BarManager.Bars 集合中。 通过 Bar.AddItem 和 Bar.AddItems 方法,Bar items 被添加到 bars 中。
为了在添加和定制 bars 及其 bar items 时避免闪烁,执行定制操作的代码被放置在 BarManager.BeginUpdate 和 BarManager.EndUpdate 方法之间进行调用。
为了对单击 bar items 作出响应, BarManager.ItemClick 事件被接管。
C# | 复制代码 |
---|---|
using DevExpress.XtraBars; private void Form1_Load(object sender, EventArgs e) { BarManager barManager = new BarManager(); barManager.Form = this; // Prevent excessive updates while adding and customizing bars and bar items. // The BeginUpdate must match the EndUpdate method. barManager.BeginUpdate(); // Create two bars and dock them to the top of the form. // Bar1 - is a main menu, which is stretched to match the form's width. // Bar2 - is a regular bar. Bar bar1 = new Bar(barManager, "My MainMenu"); Bar bar2 = new Bar(barManager, "My Bar"); bar1.DockStyle = BarDockStyle.Top; bar2.DockStyle = BarDockStyle.Top; // Position the bar1 above the bar2 bar1.DockRow = 0; // The bar1 must act as the main menu. barManager.MainMenu = bar1; // Create bar items for the bar1 and bar2 BarSubItem subMenuFile = new BarSubItem(barManager, "File"); BarSubItem subMenuEdit = new BarSubItem(barManager, "Edit"); BarSubItem subMenuView = new BarSubItem(barManager, "View"); BarButtonItem buttonOpen = new BarButtonItem(barManager, "Open"); BarButtonItem buttonExit = new BarButtonItem(barManager, "Exit"); BarButtonItem buttonCopy = new BarButtonItem(barManager, "Copy"); BarButtonItem buttonCut = new BarButtonItem(barManager, "Cut"); BarButtonItem buttonViewOutput = new BarButtonItem(barManager, "Output"); subMenuFile.AddItems(new BarItem[] { buttonOpen, buttonExit}); subMenuEdit.AddItems(new BarItem[] { buttonCopy, buttonCut}); subMenuView.AddItem(buttonViewOutput); //Add the sub-menus to the bar1 bar1.AddItems(new BarItem[] {subMenuFile, subMenuEdit, subMenuView }); // Add the buttonViewOutput to the bar2. bar2.AddItem(buttonViewOutput); // A handler to process clicks on bar items barManager.ItemClick += new ItemClickEventHandler(barManager_ItemClick); barManager.EndUpdate(); } void barManager_ItemClick(object sender, ItemClickEventArgs e) { BarSubItem subMenu = e.Item as BarSubItem; if (subMenu != null) return; MessageBox.Show("Item '" + e.Item.Caption + "' has been clicked"); } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraBars Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim barManager As BarManager = New BarManager() barManager.Form = Me ' Prevent excessive updates while adding and customizing bars and bar items. ' The BeginUpdate must match the EndUpdate method. barManager.BeginUpdate() ' Create two bars and dock them to the top of the form. ' Bar1 - is a main menu, which is stretched to match the form's width. ' Bar2 - is a regular bar. Dim bar1 As Bar = New Bar(barManager, "My MainMenu") Dim bar2 As Bar = New Bar(barManager, "My Bar") bar1.DockStyle = BarDockStyle.Top bar2.DockStyle = BarDockStyle.Top ' Position the bar1 above the bar2 bar1.DockRow = 0 ' The bar1 must act as the main menu. barManager.MainMenu = bar1 ' Create bar items for the bar1 and bar2 Dim subMenuFile As BarSubItem = New BarSubItem(barManager, "File") Dim subMenuEdit As BarSubItem = New BarSubItem(barManager, "Edit") Dim subMenuView As BarSubItem = New BarSubItem(barManager, "View") Dim buttonOpen As BarButtonItem = New BarButtonItem(barManager, "Open") Dim buttonExit As BarButtonItem = New BarButtonItem(barManager, "Exit") Dim buttonCopy As BarButtonItem = New BarButtonItem(barManager, "Copy") Dim buttonCut As BarButtonItem = New BarButtonItem(barManager, "Cut") Dim buttonViewOutput As BarButtonItem = New BarButtonItem(barManager, "Output") subMenuFile.AddItems(New BarItem() { buttonOpen, buttonExit}) subMenuEdit.AddItems(New BarItem() { buttonCopy, buttonCut}) subMenuView.AddItem(buttonViewOutput) 'Add the sub-menus to the bar1 bar1.AddItems(New BarItem() {subMenuFile, subMenuEdit, subMenuView }) ' Add the buttonViewOutput to the bar2. bar2.AddItem(buttonViewOutput) ' A handler to process clicks on bar items AddHandler barManager.ItemClick, AddressOf barManager_ItemClick barManager.EndUpdate() End Sub Private Sub barManager_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Dim subMenu As BarSubItem = TryCast(e.Item, BarSubItem) If Not subMenu Is Nothing Then Return MessageBox.Show("Item '" & e.Item.Caption & "' has been clicked") End Sub |