Back to Devexpress

How to: Delete ListBoxControl's Items That Include Specific Strings

windowsforms-9457-controls-and-libraries-editors-and-simple-controls-examples-how-to-delete-listboxcontrols-items-that-include-specific-strings.md

latest1.5 KB
Original Source

How to: Delete ListBoxControl's Items That Include Specific Strings

  • Oct 25, 2019

The following sample code declares a DeleteItems method. This method provides search of items whose display text starts with the string specified by the s parameter (“Chicago”) within the ListBoxControl item’s collection. If found, the method removes them from the items collection.

Note: this method works if no data source is bound to the list box control. Otherwise, method execution will not take place.

The image below demonstrates the ListBoxControl control’s look & feel before and after sample code execution.

csharp
using DevExpress.XtraEditors;
// ...
private void DeleteItems(ListBoxControl listBox, string s) {
    int index = listBox.FindString(s);
    if (index == -1) return;
    while (index != -1) {
        listBox.Items.RemoveAt(index);
        index = listBox.FindString(s, index);
    }
}
// ...
DeleteItems(listBoxControl1, "Chicago");
vb
Imports DevExpress.XtraEditors
' ...
Private Sub DeleteItems(ByVal listbox As ListBoxControl, ByVal s As String)
    Dim index As Integer = listbox.FindString(s)
    If (index = -1) Then Return
    Do While index >= 0
        listbox.Items.RemoveAt(index)
        index = listbox.FindString(s, index)
    Loop
End Sub
' ...
DeleteItems(ListBoxControl1, "Chicago")