windowsforms-devexpress-dot-xtrabars-dot-barmanager-1c13706a.md
Locks the BarManager object by preventing visual updates of the object and its elements until the EndUpdate method is called.
Namespace : DevExpress.XtraBars
Assembly : DevExpress.XtraBars.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
public virtual void BeginUpdate()
Public Overridable Sub BeginUpdate
The BeginUpdate and EndUpdate methods allow you to avoid flickering while performing batch modifications to the BarManager‘s settings. Once the BeginUpdate method has been called, modifying settings of the BarManager and its elements does not cause an immediate visual update. So, multiple modifications can be made to the object and its elements without a major impact on performance or screen flickering. After all the desired operations have been finished, call the EndUpdate method.
The BeginUpdate and EndUpdate methods use an internal counter to implement the update functionality. The counter’s initial value is 0. Visual updates are forbidden if the counter’s value is greater than 0, and the updates are enabled if the counter’s value is 0. The BeginUpdate method increments the counter. The EndUpdate method decrements the counter. If the counter’s new value is 0, an immediate visual update occurs. Each call to BeginUpdate must be paired with a call to EndUpdate. To ensure that EndUpdate is always called even if an exception occurs, use the try…finally statement.
The following code shows how to create bars and bar items in code.
In the example two bars are created - a main menu (a bar that is stretched to match the form’s width), and a regular bar. The main menu will contain three sub-menus (File, Edit and View), each having its own bar items. The second bar will display the Output button, which is also available via the View submenu of the first bar:
Bars are added to the BarManager.Bars collection. Bar items are added to bars via the Bar.AddItem and Bar.AddItems methods.
To avoid flickering while adding and customizing bars and bar items, the code that performs the customization is enclosed with the BarManager.BeginUpdate and BarManager.EndUpdate method calls.
To respond to clicking bar items, the BarManager.ItemClick event is handled.
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");
}
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
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the BeginUpdate() method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
if (ribbonControl.Manager != null) {
ribbonControl.Manager.BeginUpdate();
}
See Also