Back to Devexpress

RepositoryItemComboBox.Items Property

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemcombobox-5a6d37be.md

latest8.7 KB
Original Source

RepositoryItemComboBox.Items Property

Provides access to the collection of items displayed in the editor’s dropdown.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Data")]
public virtual ComboBoxItemCollection Items { get; }
vb
<DXCategory("Data")>
Public Overridable ReadOnly Property Items As ComboBoxItemCollection

Property Value

TypeDescription
ComboBoxItemCollection

The collection of items displayed by the editor.

|

Remarks

The Items collection stores items displayed in the editor’s dropdown.

Elements in the Items collection can be of any type. The elements’ text representation is specified by their ToString() methods.

csharp
comboBoxEdit1.Properties.Items.Add("Red");
comboBoxEdit1.Properties.Items.Add("Yellow");
comboBoxEdit1.Properties.Items.Add("Green");

comboBoxEdit2.Properties.Items.Add(Color.Black);
comboBoxEdit2.Properties.Items.Add(Color.White);

Person p1 = new Person() { Name = "Walter" };
Person p2 = new Person() { Name = "Margaret" };
Person p3 = new Person() { Name = "Janet" };
comboBoxEdit3.Properties.Items.Add(p1);
comboBoxEdit3.Properties.Items.Add(p2);
comboBoxEdit3.Properties.Items.Add(p3);
class Person {
    public string Name { get; set; }
    public override string ToString() {
        return Name;
    }
}
vb
ComboBoxEdit1.Properties.Items.Add("Red")
ComboBoxEdit1.Properties.Items.Add("Yellow")
ComboBoxEdit1.Properties.Items.Add("Green")

ComboBoxEdit2.Properties.Items.Add(Color.Black)
ComboBoxEdit2.Properties.Items.Add(Color.White)

Dim p1 As Person = New Person() With {.Name = "Walter"}
Dim p2 As Person = New Person() With {.Name = "Margaret"}
Dim p3 As Person = New Person() With {.Name = "Janet"}
ComboBoxEdit3.Properties.Items.Add(p1)
ComboBoxEdit3.Properties.Items.Add(p2)
ComboBoxEdit3.Properties.Items.Add(p3)

Class Person
    Public Property Name As String
    Public Overrides Function ToString() As String
        Return Name
    End Function
End Class

Note

The editor’s items must be unique objects.

When an end-user selects a row in the dropdown, the editor assigns a corresponding item from the Items collection to the ComboBoxEdit.SelectedItem property.

To select a specific item, you can use the following approaches:

csharp
comboBoxEdit1.SelectedIndex = 1;
comboBoxEdit2.SelectedItem = Color.White;
comboBoxEdit3.EditValue = p3;
vb
comboBoxEdit1.SelectedIndex = 1;
comboBoxEdit2.SelectedItem = Color.White;
comboBoxEdit3.EditValue = p3;

At design time, you can populate the Items collection with string elements in the Visual Studio Property Grid. Other item types can only be added in code.

Example: Set ComboBox Bar Item Value in Ribbon

csharp
using System;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Repository;

public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm {
    BarEditItem barEditItemComboBox;
    public Form1() {
        InitializeComponent();
        // Creates a ComboBox repository item.
        RepositoryItemComboBox comboBoxSettings = new RepositoryItemComboBox();
        // Adds items to the 'Items' colelction.
        comboBoxSettings.Items.AddRange(new string[] { "Item A", "Item B", "Item C" });
        // Creates and initializes a BarEditItem.
        barEditItemComboBox = new BarEditItem(ribbonControl1.Manager, comboBoxSettings);
        // Specifies the width of the ComboBox.
        barEditItemComboBox.EditWidth = 140;
        // Adds the BarEditItem to a Ribbon group.
        ribbonControl1.Pages[0].Groups[0].ItemLinks.Add(barEditItemComboBox);
    }
    private void Form1_Load(object sender, EventArgs e) {
        // Sets the editor's value.
        barEditItemComboBox.EditValue = "Item B";
    }
}
vb
Imports System
Imports DevExpress.XtraBars
Imports DevExpress.XtraEditors.Repository

Partial Public Class Form1
    Inherits DevExpress.XtraBars.Ribbon.RibbonForm

    Private barEditItemComboBox As BarEditItem
    Public Sub New()
        InitializeComponent()
        ' Creates a ComboBox repository item.
        Dim comboBoxSettings As New RepositoryItemComboBox()
        ' Adds items to the 'Items' colelction.
        comboBoxSettings.Items.AddRange(New String() { "Item A", "Item B", "Item C" })
        ' Creates and initializes a BarEditItem.
        barEditItemComboBox = New BarEditItem(ribbonControl1.Manager, comboBoxSettings)
        ' Specifies the width of the ComboBox.
        barEditItemComboBox.EditWidth = 140
        ' Adds the BarEditItem to a Ribbon group.
        ribbonControl1.Pages(0).Groups(0).ItemLinks.Add(barEditItemComboBox)
    End Sub
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Sets the editor's value.
        barEditItemComboBox.EditValue = "Item B"
    End Sub
End Class

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

See Also

SelectedItem

RepositoryItemComboBox Class

RepositoryItemComboBox Members

DevExpress.XtraEditors.Repository Namespace