ComboBox.FindString 方法

定義

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。Returns the index of the first item in the ComboBox that starts with the specified string.

多載

FindString(String)

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。Returns the index of the first item in the ComboBox that starts with the specified string.

FindString(String, Int32)

傳回 ComboBox 中指定索引位置之外包含指定字串之第一個項目的索引。Returns the index of the first item in the ComboBox beyond the specified index that contains the specified string. 搜尋是不區分大小寫的。The search is not case sensitive.

FindString(String)

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。Returns the index of the first item in the ComboBox that starts with the specified string.

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

參數

s
String

要搜尋的 StringThe String to search for.

傳回

找到第一個項目之以零為起始的索引,如果沒有找到符合的,則傳回 -1。The zero-based index of the first item found; returns -1 if no match is found.

範例

下列程式碼範例顯示 FindString 方法和 SelectedIndex 屬性的用法。The following code example shows the usage of the FindString method and SelectedIndex property. 此範例是 ComboBox 類別總覽中完整程式碼範例的一部分。The example is part of a complete code example in the ComboBox class overview.

void findButton_Click( Object^ sender, System::EventArgs^ e )
{
   int index = comboBox1->FindString( textBox2->Text );
   comboBox1->SelectedIndex = index;
}
private void findButton_Click(object sender, System.EventArgs e) {
    int index = comboBox1.FindString(textBox2.Text);
    comboBox1.SelectedIndex = index;
}
Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim index As Integer
    index = comboBox1.FindString(textBox2.Text)
    comboBox1.SelectedIndex = index
End Sub

備註

這個方法所執行的搜尋不區分大小寫。The search performed by this method is not case-sensitive. s 參數是要與下拉式方塊清單中的專案相關聯的文字進行比對的子字串。The s parameter is a substring to compare against the text associated with the items in the combo box list. 搜尋會從文字開頭開始執行部分比對,並傳回清單中符合指定之子字串的第一個專案。The search performs a partial match starting from the beginning of the text, and returning the first item in the list that matches the specified substring. 接著,您可以執行工作,例如使用 Remove 方法或變更專案的文字,移除包含搜尋文字的專案。You can then perform tasks, such as removing the item that contains the search text using the Remove method or changing the item's text. 找到指定的文字之後,如果您想要搜尋 ComboBox中的其他文字範例,就必須使用提供參數的 FindString 方法版本,以便在 ComboBox內指定起始索引。Once you have found the specified text, if you want to search for other instances of the text in the ComboBox, you must use the version of the FindString method that provides a parameter for specifying a starting index within the ComboBox. 如果您想要搜尋完全相符的單字,而不是部分相符,請使用 FindStringExact 方法。If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

FindString(String, Int32)

傳回 ComboBox 中指定索引位置之外包含指定字串之第一個項目的索引。Returns the index of the first item in the ComboBox beyond the specified index that contains the specified string. 搜尋是不區分大小寫的。The search is not case sensitive.

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

參數

s
String

要搜尋的 StringThe String to search for.

startIndex
Int32

搜尋第一個項目之前,項目以零為起始的索引。The zero-based index of the item before the first item to be searched. 設定為 -1 以便從控制項的開頭進行搜尋。Set to -1 to search from the beginning of the control.

傳回

找到的第一個項目之以零為起始的索引;如果找不到相符項,則傳回 -1;如果 s 參數指定 Empty,則傳回 0。The zero-based index of the first item found; returns -1 if no match is found, or 0 if the s parameter specifies Empty.

例外狀況

startIndex 小於 -1。The startIndex is less than -1.

-或--or- startIndex 大於集合中的最後索引。The startIndex is greater than the last index in the collection.

備註

這個方法所執行的搜尋不區分大小寫。The search performed by this method is not case-sensitive. s 參數是要與下拉式方塊清單中的專案相關聯的文字進行比對的子字串。The s parameter is a substring to compare against the text associated with the items in the combo box list. 搜尋會從文字開頭開始執行部分比對,並傳回清單中符合指定之子字串的第一個專案。The search performs a partial match starting from the beginning of the text, returning the first item in the list that matches the specified substring. 接著,您可以執行工作,例如使用 Remove 方法或變更專案的文字,移除包含搜尋文字的專案。You can then perform tasks, such as removing the item that contains the search text using the Remove method or changing 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. 在清單中找到初始專案之後,這個方法通常用來尋找搜尋文字的進一步實例,方法是在第一個找到的搜尋文字範例之後,于專案的 startIndex 參數中指定索引位置。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. 如果您想要搜尋完全相符的單字,而不是部分相符,請使用 FindStringExact 方法。If you want to perform a search for an exact word match instead of a partial match, use the FindStringExact method.

適用於