Back to Devexpress

ComboBoxItemCollection.Add(Object) Method

windowsforms-devexpress-dot-xtraeditors-dot-controls-dot-comboboxitemcollection-dot-add-x28-system-dot-object-x29.md

latest8.9 KB
Original Source

ComboBoxItemCollection.Add(Object) Method

Adds a new item to the current collection.

Namespace : DevExpress.XtraEditors.Controls

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public virtual int Add(
    object item
)
vb
Public Overridable Function Add(
    item As Object
) As Integer

Parameters

NameTypeDescription
itemObject

The object representing the new item in the collection.

|

Returns

TypeDescription
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.

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

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.

winforms-bars-create-baredititem/CS/Form1.cs#L139

csharp
combo = barManager1.RepositoryItems.Add("ComboBoxEdit") as DevExpress.XtraEditors.Repository.RepositoryItemComboBox;
combo.Items.Add("Item A");
combo.Items.Add("Item B");

how-to-keep-a-look-and-feel-of-windows-forms-in-sync-e2243/CS/ThreadedSkinning/Form1.cs#L20

csharp
foreach(DevExpress.Skins.SkinContainer cnt in DevExpress.Skins.SkinManager.Default.Skins) {
    comboBoxEdit1.Properties.Items.Add(cnt.SkinName);
}

winforms-grid-multi-cell-editing/CS/Form1.cs#L30

csharp
for (int i = 0; i < 10; i++)
        ri.Items.Add(String.Format("Test{0}", i));
}

winforms-mvvm-expenses-app/CS/MVVMExpenses/Views/LoginView.cs#L28

csharp
foreach(string item in mvvmContext1.GetViewModel<LoginViewModel>().LookUpUsers)
    LoginTextEdit.Properties.Items.Add(item);
fluentAPI.ViewModel.Init();

reporting-winforms-custom-controls-for-editing-report-content-in-print-preview/CS/RegisterCustomFieldEditor/Form1.cs#L22

csharp
for (int i = 0; i < 20; i++)
    repositoryItemComboBox.Items.Add(string.Format("Item {0}", i + 1));
EditingFieldExtensionsWin.Instance.RegisterEditor

winforms-bars-create-baredititem/VB/Form1.vb#L134

vb
combo = TryCast(barManager1.RepositoryItems.Add("ComboBoxEdit"), DevExpress.XtraEditors.Repository.RepositoryItemComboBox)
combo.Items.Add("Item A")
combo.Items.Add("Item B")

how-to-keep-a-look-and-feel-of-windows-forms-in-sync-e2243/VB/ThreadedSkinning/Form1.vb#L17

vb
For Each cnt As DevExpress.Skins.SkinContainer In DevExpress.Skins.SkinManager.Default.Skins
    comboBoxEdit1.Properties.Items.Add(cnt.SkinName)
Next

winforms-grid-multi-cell-editing/VB/Form1.vb#L30

vb
For i As Integer = 0 To 10 - 1
    ri.Items.Add(String.Format("Test{0}", i))
Next

winforms-mvvm-expenses-app/VB/MVVMExpenses/Views/LoginView.vb#L30

vb
For Each item As String In mvvmContext1.GetViewModel(Of LoginViewModel)().LookUpUsers
    LoginTextEdit.Properties.Items.Add(item)
Next item

reporting-winforms-custom-controls-for-editing-report-content-in-print-preview/VB/RegisterCustomFieldEditor/Form1.vb#L21

vb
For i As Integer = 0 To 20 - 1
    repositoryItemComboBox.Items.Add(String.Format("Item {0}", i + 1))
Next

See Also

AddRange

ComboBoxItemCollection Class

ComboBoxItemCollection Members

DevExpress.XtraEditors.Controls Namespace