FindStringExact Metoda (String, Int32)
Zwiń spis treści
Rozwiń spis treści
Ten artykuł został przetłumaczony przez maszynę. Aby wyświetlić jego treść w języku angielskim, zaznacz pole wyboru Angielski. Możesz też wyświetlić angielski tekst w okienku wyskakującym, przesuwając wskaźnik myszy nad konkretny tekst”.

Metoda ListBox.FindStringExact — (String, Int32)

.NET Framework (current version)

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

Przestrzeń nazw:   System.Windows.Forms
Zestaw:  System.Windows.Forms (w System.Windows.Forms.dll)

public int FindStringExact(
	string s,
	int startIndex



The text to search for.


The zero-based index of the item before the first item to be searched.Set to negative one (-1) to search from the beginning of the control.

Wartość zwracana

Type: System.Int32

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

Exception Condition

The startIndex parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

The search performed by this method is not case-sensitive.The search looks for words that exactly match the specified search string parameter, s.You can use this method to search for the first item that matches the specified string at the specified starting index within the list of items for the ListBox.You can then perform tasks such as removing the item that contains the search text using the Remove method or change the item's text.This method is typically used after a call has been made using the version of this method that does not specify a starting index.Once an initial item has been found in the list, this method is typically used to find further instances of the search text by specifying the index position in the startIndex parameter of the item after the first found instance of the search text.If you want to perform a partial word search instead of an exact word match, use the FindString method.


When the search reaches the bottom of the ListBox, it continues searching from the top of the ListBox back to the item specified by the startIndex parameter.

The following code 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.

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.
         // 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])
            // Select the item in the ListBox once it is found.

      }while(x != -1);

.NET Framework
Dostêpne od 1.1
Powrót do początku
© 2016 Microsoft