ComboBoxItemCollection.Add(Object) Method
Adds a new item to the current collection.
Namespace: DevExpress.XtraEditors.Controls
Assembly: DevExpress.XtraEditors.v26.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
Parameters
| Name | Type | Description |
|---|---|---|
| item | Object | The object representing the new item in the collection. |
Returns
| Type | Description |
|---|---|
| Int32 | The position to which the new element was inserted. |
Remarks
The Add method adds a new item at the end of the current collection. The object representing the item is passed as the item parameter. Whenever you select a specific element from the dropdown list in a combo box editor, a corresponding object is assigned to the ComboBoxEdit.SelectedItem property and the edit box displays the textual representation of the object.
To add several objects at once, see the ComboBoxItemCollection.AddRange method.
Note: to ensure proper functionality of the editor, items in the RepositoryItemComboBox.Items collection must be unique objects.
Example
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;
}
}