This documentation is archived and is not being maintained.

ListBox.FindStringExact Method

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

Overload List

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

[Visual Basic] Overloads Public Function FindStringExact(String) As Integer
[C#] public int FindStringExact(string);
[C++] public: int FindStringExact(String*);
[JScript] public function FindStringExact(String) : int;

Finds the first item in the ListBox that exactly matches the specified string. The search starts at a specific starting index.

[Visual Basic] Overloads Public Function FindStringExact(String, Integer) As Integer
[C#] public int FindStringExact(string, int);
[C++] public: int FindStringExact(String*, int);
[JScript] public function FindStringExact(String, int) : int;

Example

[Visual Basic, C#, C++] The following example demonstrates how to use the FindStringExact method to search for all items in a ListBox that exactly match the specified search text. The example uses the version of the FindStringExact method that enables you to specify a starting search index from which to do a continual search of all items in the ListBox. The example also demonstrates how to determine when the FindStringExact method begins searching from the top of the list after it reaches the bottom of the list of items, to prevent a recursive search. Once items are found in the ListBox, they are selected using the SetSelected method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of FindStringExact. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub FindAllOfMyExactStrings(ByVal searchString As String)
   ' Set the SelectionMode property of the ListBox to select multiple items.
   ListBox1.SelectionMode = SelectionMode.MultiExtended

   ' Set our intial index variable to -1.
   Dim x As Integer = -1
   ' If the search string is empty exit.
   If searchString.Length <> 0 Then
      ' Loop through and find each item that matches the search string.
      Do
         ' Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = ListBox1.FindStringExact(searchString, x)
         ' If no item is found that matches exit.
         If x <> -1 Then
            ' Since the FindStringExact loops infinitely, determine if we found first item again and exit.
            If ListBox1.SelectedIndices.Count > 0 Then
               If x = ListBox1.SelectedIndices(0) Then
                  Return
               End If
            End If
            ' Select the item in the ListBox once it is found.
            ListBox1.SetSelected(x, True)
         End If
      Loop While x <> -1
   End If
End Sub

[C#] 
private void FindAllOfMyExactStrings(string searchString)
{
   // Set the SelectionMode property of the ListBox to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;
   
   // Set our intial index variable to -1.
   int x =-1;
   // If the search string is empty exit.
   if (searchString.Length != 0)
   {
      // Loop through and find each item that matches the search string.
      do
      {
         // Retrieve the item based on the previous index found. Starts with -1 which searches start.
         x = listBox1.FindStringExact(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindStringExact loops infinitely, determine if we found first item again and exit.
            if (listBox1.SelectedIndices.Count > 0)
            {
               if (x == listBox1.SelectedIndices[0])
                  return;
            }
            // Select the item in the ListBox once it is found.
            listBox1.SetSelected(x,true);
         }
   
      }while(x != -1);
   }
}

[C++] 
private:
   void FindAllOfMyExactStrings(String* searchString)
   {
      // Set the SelectionMode property of the ListBox to select multiple items.
      listBox1->SelectionMode = SelectionMode::MultiExtended;

      // Set our intial index variable to -1.
      int x =-1;
      // If the search string is empty exit.
      if (searchString->Length != 0)
      {
         // Loop through and find each item that matches the search string.
         do
         {
            // Retrieve the item based on the previous index found. Starts with -1 which searches start.
            x = listBox1->FindStringExact(searchString, x);
            // If no item is found that matches exit.
            if (x != -1)
            {
               // Since the FindStringExact loops infinitely, determine if we found first item again and exit.
               if (listBox1->SelectedIndices->Count > 0)
               {
                  if (x == listBox1->SelectedIndices->Item[0])
                     return;
               }
               // Select the item in the ListBox once it is found.
               listBox1->SetSelected(x,true);
            }

         }while(x != -1);
      }
   }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

ListBox Class | ListBox Members | System.Windows.Forms Namespace

Show: