windowsforms-devexpress-dot-xtraeditors-dot-controls-dot-comboboxitemcollection.md
Locks the ComboBoxItemCollection by preventing change notifications from being fired, preventing visual updates until the EndUpdate method is called.
Namespace : DevExpress.XtraEditors.Controls
Assembly : DevExpress.XtraEditors.v25.2.dll
NuGet Package : DevExpress.Win.Navigation
public virtual void BeginUpdate()
Public Overridable Sub BeginUpdate
Use BeginUpdate along with the EndUpdate method to prevent superfluous change notifications from being sent while performing multiple changes on the ComboBoxItemCollection. Enclose the code that performs multiple changes on the collection with the BeginUpdate and EndUpdate methods. The result will be that the change notification will fire only once, triggered by the EndUpdate method.
The BeginUpdate and EndUpdate methods use an internal counter to implement the update functionality. The counter’s initial value is 0. The BeginUpdate method increments the counter. EndUpdate decrements the counter, and if its new value is zero, change notification is enabled. Note that each call to BeginUpdate must be paired with a call to EndUpdate. If you call BeginUpdate but forget to call EndUpdate afterwards or EndUpdate is not called because an exception occurred, change notifications will no longer be fired. To ensure that EndUpdate is always called even if an exception occurs, use the try…finally statement.
The following code creates a ComboBoxEdit control, and adds three items to the control. Items are PersonInfo class objects. Each item stores a person’s first and last names.
The example uses the ComboBoxItemCollection.BeginUpdate and ComboBoxItemCollection.EndUpdate methods to prevent excessive updates when the item collection is changed.
The ComboBoxEdit.SelectedIndex property is set to -1 for demonstration purposes (the property is initially set to -1). This ensures that no item is selected on application startup.
ComboBoxEdit combo = new ComboBoxEdit();
ComboBoxItemCollection coll = combo.Properties.Items;
coll.BeginUpdate();
try {
coll.Add(new PersonInfo("Sven", "Petersen"));
coll.Add(new PersonInfo("Cheryl", "Saylor"));
coll.Add(new PersonInfo("Dirk", "Luchte"));
}
finally {
coll.EndUpdate();
}
combo.SelectedIndex = -1;
Controls.Add(combo);
//...
public class PersonInfo {
private string _firstName;
private string _lastName;
public PersonInfo(string firstName, string lastName) {
_firstName = firstName;
_lastName = lastName;
}
public override string ToString() {
return _firstName + " " + _lastName;
}
}
Dim Combo As ComboBoxEdit = New ComboBoxEdit
Dim Coll As ComboBoxItemCollection = Combo.Properties.Items
Coll.BeginUpdate()
Try
Coll.Add(New PersonInfo("Sven", "Petersen"))
Coll.Add(New PersonInfo("Cheryl", "Saylor"))
Coll.Add(New PersonInfo("Dirk", "Luchte"))
Finally
Coll.EndUpdate()
End Try
Combo.SelectedIndex = -1
Controls.Add(Combo)
'...
Public Class PersonInfo
Private _firstName As String
Private _lastName As String
Public Sub New(ByVal firstName As String, ByVal lastName As String)
_firstName = firstName
_lastName = lastName
End Sub
Public Overrides Function ToString() As String
Return _firstName + " " + _lastName
End Function
End Class
See Also