Nyelv

ListBox.FindString Metódus

Definíció

Megkeresi a megadott sztringgel kezdődő első elemet ListBox .

Túlterhelések

Név Description
FindString(String)

Megkeresi a megadott sztringgel kezdődő első elemet ListBox .

FindString(String, Int32)

Megkeresi a megadott sztringgel kezdődő első elemet ListBox . A keresés egy adott kezdőindexnél kezdődik.

FindString(String)

Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs

Megkeresi a megadott sztringgel kezdődő első elemet ListBox .

public:
 int FindString(System::String ^ s);
public int FindString(string s);
member this.FindString : string -> int
Public Function FindString (s As String) As Integer

Paraméterek

s
String

A keresendő szöveg.

Válaszok

Az első talált elem nulla alapú indexe; visszaadja, ListBox.NoMatches ha nem található egyezés.

Kivételek

A paraméter értéke s kisebb, mint -1 vagy nagyobb, mint az elemszám.

Példák

Az alábbi példakód bemutatja, hogyan használható a metódus egy FindString sztring első példányának keresésére egy ListBox. Ha nem található olyan elem, amely megfelel a keresési sztringnek FindString , -1 értéket ad vissza, és a példa egy MessageBox. Ha olyan elemet talál, amely megfelel a keresési szövegnek, a példa a SetSelected metódus használatával választja ki az elemet a ListBoxlistában.

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 (!string.IsNullOrEmpty(searchString))
   {
      // 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 Sub FindMyString(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.FindString(searchString)
      ' Determine if a valid index is returned. Select the item if it is valid.
      If index <> -1 Then
         listBox1.SetSelected(index, True)
      Else
         MessageBox.Show("The search string did not match any items in the ListBox")
      End If
   End If
End Sub

Megjegyzések

Az ezzel a módszerrel végzett keresés nem megkülönbözteti a kis- és nagybetűk adatait. A keresés olyan szavakat keres, amelyek részben egyeznek a megadott keresési sztringparaméterrel. s Ezzel a módszerrel megkeresheti a megadott sztringnek megfelelő első elemet. Ezután olyan műveleteket hajthat végre, mint például a keresési szöveget tartalmazó elem eltávolítása a Remove metódussal vagy az elem szövegének módosításával. Miután megtalálta a megadott szöveget, ha a szöveg más példányait is meg szeretné keresni a ListBoxfájlban, használhatja a FindString metódus azon verzióját, amely paramétert biztosít a kezdő index megadásához a ListBoxfájlban. Ha pontos szóegyezést szeretne keresni részleges egyezés helyett, használja a metódust FindStringExact .

Lásd még

A következőre érvényes:

FindString(String, Int32)

Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs
Forrás:
ListBox.cs

Megkeresi a megadott sztringgel kezdődő első elemet ListBox . A keresés egy adott kezdőindexnél kezdődik.

public:
 int FindString(System::String ^ s, int startIndex);
public int FindString(string s, int startIndex);
member this.FindString : string * int -> int
Public Function FindString (s As String, startIndex As Integer) As Integer

Paraméterek

s
String

A keresendő szöveg.

startIndex
Int32

Az elem nulla alapú indexe az első keresendő elem előtt. Állítsa negatívra (-1) a vezérlőelem elejétől való kereséshez.

Válaszok

Az első talált elem nulla alapú indexe; visszaadja, ListBox.NoMatches ha nem található egyezés.

Kivételek

A startIndex paraméter kisebb, mint nulla vagy nagyobb, mint az osztály tulajdonságának CountListBox.ObjectCollection értéke.

Példák

Az alábbi példakód bemutatja, hogyan használható a metódus a FindString keresési szöveg összes példányának keresésére a ListBoxfájl elemeiben. A példa a metódus azon verzióját FindString használja, amely lehetővé teszi egy kezdő keresési index megadását, amely alapján folyamatosan keresheti az összes elemet a ListBoxfájlban. A példa azt is bemutatja, hogyan állapítható meg, hogy a FindString metódus mikor kezdi meg a keresést a lista tetejéről, miután elérte az elemek listájának alját, hogy megakadályozza a rekurzív keresést. Miután megtalálta az elemeket a ListBoxfájlban, a rendszer kiválasztja őket a SetSelected módszerrel.

private:
   void FindAllOfMyString( 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->FindString( searchString, x );

            // If no item is found that matches exit.
            if ( x != -1 )
            {
               // Since the FindString 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 );
      }
   }
private void FindAllOfMyString(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.FindString(searchString, x);
         // If no item is found that matches exit.
         if (x != -1)
         {
            // Since the FindString 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);
   }
}
Private Sub FindAllOfMyString(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.FindString(searchString, x)
         ' If no item is found that matches exit.
         If x <> -1 Then
            ' Since the FindString 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

Megjegyzések

Az ezzel a módszerrel végzett keresés nem megkülönbözteti a kis- és nagybetűk adatait. A keresés olyan szavakat keres, amelyek részben egyeznek a megadott keresési sztringparaméterrel. s Ezzel a módszerrel megkeresheti az első elemet, amely megfelel a megadott sztringnek a megadott kezdőindexben, a ListBoxlista elemeinek listájában. Ezután olyan műveleteket hajthat végre, mint például a keresési szöveget tartalmazó elem eltávolítása a Remove metódussal vagy az elem szövegének módosításával. Ezt a metódust általában akkor használják, ha a hívás a metódus azon verziójával történt, amely nem ad meg kezdő indexet. Miután egy kezdeti elemet talált a listában, ez a módszer általában a keresési szöveg további példányainak megkeresésére szolgál azáltal, hogy megadja az indexpozíciót az elem paraméterében startIndex a keresési szöveg első megtalált példánya után. Ha pontos szóegyezést szeretne keresni részleges egyezés helyett, használja a metódust FindStringExact .

Note

Amikor a keresés eléri a lap alját ListBox, a rendszer a háttér tetejétől ListBox a paraméter által megadott elemig folytatja a startIndex keresést.

Lásd még

A következőre érvényes: