Skip to main content

BaseListBoxControl.ItemCount Property

Gets the number of elements contained in the collection of the list box control.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

Declaration

[Browsable(false)]
public virtual int ItemCount { get; }

Property Value

Type Description
Int32

An integer value representing the number of elements contained in the collection.

Remarks

Use this property to determine the number of items displayed in the list box control. This can be useful when you need to traverse through the elements contained in the collection. In this case, read the ItemCount property value to obtain the number of iterations to be performed.

When a list box control is bound to a data source via the BaseListBoxControl.DataSource property, its Items collection doesn’t provide access to the data source’s items. So, the Items.Count property doesn’t allow you to get the number of items in the data source. To get the number of items, use the ItemCount property instead. When the list box control is not bound to a data source, the Items.Count and ItemCount properties are in sync.

Example

The following sample code declares a DeleteItems method accepting two parameters:

  • listBoxControl - a ListBoxControl;
  • index - the index of an item after which all items are to be removed.

Use this method to delete all items from the ListBoxControl items collection that are next to the specified one. If the listBoxControl parameter points to a non-existent control or the index parameter points either to the non-existent control or the last item in the collection, method execution will not take place.

Note: if the control specified by the listBoxControl parameter is bound to a data source, method execution will not take place.

private void DeleteItems(ListBoxControl listBox, int index){
   int count = listBox.ItemCount;
   if (index < listBox.ItemCount - 1)
      for (int i = index + 1; i < count; i ++)
         listBox.Items.RemoveAt(index + 1);
}
// ...
// Deletes all items from the ListBoxControl collection after the selected one
DeleteItems(listBoxControl1, listBoxControl1.SelectedIndex);
See Also