Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

BaseListBoxControl.FindString(String, Int32) Method

Finds the first item in the list box control which starts with the specified string. The search starts at a starting index specified by the parameter.

Namespace: DevExpress.XtraEditors

Assembly: DevExpress.XtraEditors.v24.2.dll

NuGet Package: DevExpress.Win.Navigation

#Declaration

public int FindString(
    string s,
    int startIndex
)

#Parameters

Name Type Description
s String

A string value providing the search text.

startIndex Int32

An integer value providing the zero-based index of the start item.

#Returns

Type Description
Int32

An integer value representing the zero-based index of the first item found. -1 if no item is found.

#Remarks

This method searches for the first item found that starts with the specified string. The startIndex parameter specifies the index from which the search starts. Set it to -1 to start with the first entry. Note that the search is not case-sensitive.

Usually, your first search will not provide a startIndex, but use this method syntax to find further instances of the search text.

If you want to search for an exact text instead of a partial match, use the BaseListBoxControl.FindStringExact method.

#Example

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.

BaseListBoxControl - FindString

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");
See Also