Export (0) Print
Expand All

ListBox.FindStringExact Method (String)

Finds the first item in the ListBox that exactly matches the specified string.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Function FindStringExact ( _
	s As String _
) As Integer

Parameters

s
Type: System.String

The text to search for.

Return Value

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

The search performed by this method is not case-sensitive. The search looks for an exact match to the words specified in the 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 FindStringExact method that provides a parameter for specifying a starting index within the ListBox. If you want to perform partial word search instead of an exact word match, use the FindString method.

The following code example demonstrates how to use the ListBox.FindStringExact method to search a ListBox control for an item that exactly matches a specified string. If no items are found that match the search string, FindStringExact 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 Sub FindMySpecificString(ByVal searchString As String)
   ' Ensure we have a proper string to search for. 
   If searchString <> String.Empty Then 
      ' Find the item in the list and store the index to the item. 
      Dim index As Integer = listBox1.FindStringExact(searchString)
      ' Determine if a valid index is returned. Select the item if it is valid. 
      If index <> ListBox.NoMatches Then
         listBox1.SetSelected(index, True)
      Else
         MessageBox.Show("The search string did not find any items in the ListBox that exactly match the specified search string")
      End If 
   End If 
End Sub

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft