Export (0) Print
Expand All

ListBox.FindString Method (String)

Finds the first item in the ListBox that starts with the specified string.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public int FindString (
	string s
)
public int FindString (
	String s
)
public function FindString (
	s : String
) : int
Not applicable.

Parameters

s

The text to search for.

Return Value

The zero-based index of the first item found; returns ListBox.NoMatches if no match is found.

Exception typeCondition

ArgumentOutOfRangeException

The value of the s parameter is less than -1 or greater than or equal to the item count.

The search performed by this method is not case-sensitive. The search looks for words that partially match the specified search string parameter, s. You can use this method to search for the first item that matches the specified string. You can then perform tasks such as removing the item that contains the search text by using the Remove method or changing the item's text. Once you have found the specified text, if you want to search for other instances of the text in the ListBox, you can use the version of the FindString method that provides a parameter for specifying a starting index within the ListBox. If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

The following code example demonstrates how to use the FindString method to search for the first instance of a string in a ListBox. If no items are found that match the search string FindString returns a -1 value and the example displays a MessageBox. If an item is found that matches the search text, the example uses the SetSelected method to select the item in the ListBox.

private void FindMyString(string searchString)
{
   // Ensure we have a proper string to search for.
   if (searchString != string.Empty)
   {
      // Find the item in the list and store the index to the item.
      int index = listBox1.FindString(searchString);
      // Determine if a valid index is returned. Select the item if it is valid.
      if (index != -1)
         listBox1.SetSelected(index,true);
      else
         MessageBox.Show("The search string did not match any items in the ListBox");
   }
}

private void FindMyString(String searchString)
{
    // Ensure we have a proper string to search for.
    if (!searchString.Equals("")) {
        // Find the item in the list and store the index to the item.
        int index = listBox1.FindString(searchString);
        // Determine if a valid index is returned. 
        // Select the item if it is valid.
        if (index != -1) {
            listBox1.SetSelected(index, true);
        }
        else {
            MessageBox.Show("The search string did not match "
                + "any items in the ListBox");
        }
    }
} //FindMyString

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft