Back to Devexpress

BaseListBoxControl.ItemCount Property

windowsforms-devexpress-dot-xtraeditors-dot-baselistboxcontrol-35681c61.md

latest3.6 KB
Original Source

BaseListBoxControl.ItemCount Property

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

Namespace : DevExpress.XtraEditors

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[Browsable(false)]
public virtual int ItemCount { get; }
vb
<Browsable(False)>
Public Overridable ReadOnly Property ItemCount As Integer

Property Value

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

csharp
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);
vb
Private Sub DeleteItems(ByVal listBox As ListBoxControl, ByVal index As Integer)
   Dim count As Integer
   Dim i As Integer
   count = listBox.ItemCount
   If index < listBox.ItemCount - 1 Then
      For i = index + 1 To count - 1
         listBox.Items.RemoveAt(index + 1)
      Next i
   End If
End Sub
' ...
' Deletes all items from the ListBoxControl collection after the selected one
DeleteItems(ListBoxControl1, ListBoxControl1.SelectedIndex)

See Also

BaseListBoxControl Class

BaseListBoxControl Members

DevExpress.XtraEditors Namespace