Skip to main content
A newer version of this page is available. .

ComboBoxItemCollection.Add(Object) Method

Adds a new item to the current collection.

Namespace: DevExpress.XtraEditors.Controls

Assembly: DevExpress.XtraEditors.v19.2.dll

Declaration

public virtual int Add(
    object item
)

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 combo box editor and adds three items to the item collection. Each item in the example is a PersonInfo class object, which stores a person’s first and last names.

The ComboBoxItemCollection.BeginUpdate and ComboBoxItemCollection.EndUpdate methods are called to prevent excessive updates when the item collection is changed (three items are added).

The BaseListBoxControl.SelectedIndex property is set to -1 for demonstration purposes (the property is set to -1 by default). This ensures that no item is currently selected in the combo box.

  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;
    }
  }

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(Object) 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.

See Also