ComboBox.FindString 方法

定義

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。

多載

FindString(String)

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。

FindString(String, Int32)

傳回 ComboBox 中指定索引位置之外包含指定字串之第一個項目的索引。 搜尋是不區分大小寫的。

FindString(String)

傳回 ComboBox 中以指定字串開頭之第一個項目的索引。

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

參數

s
String

要搜尋的 String

傳回

找到第一個項目之以零為起始的索引,如果沒有找到符合的,則傳回 -1。

範例

下列程式碼範例顯示 方法和 SelectedIndex 屬性的使用方式 FindString 。 此範例是類別概觀中完整程式碼範例的 ComboBox 一部分。

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

備註

這個方法執行的搜尋不會區分大小寫。 參數 s 是子字串,可與下拉式方塊清單中的專案相關聯的文字進行比較。 搜尋會從文字開頭開始執行部分比對,並傳回清單中符合指定子字串的第一個專案。 然後,您可以執行工作,例如使用 Remove 方法來移除包含搜尋文字的專案,或變更專案的文字。 找到指定的文字之後,如果您想要在 中 ComboBox 搜尋其他文字範例,則必須使用 FindString 提供參數的 方法來指定 中的 ComboBox 起始索引。 如果您想要執行搜尋完全相符的字組,而不是部分比對,請使用 FindStringExact 方法。

適用於

FindString(String, Int32)

傳回 ComboBox 中指定索引位置之外包含指定字串之第一個項目的索引。 搜尋是不區分大小寫的。

public:
 int FindString(System::String ^ s, int startIndex);
public int FindString (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

要搜尋的 String

startIndex
Int32

搜尋第一個項目之前,項目以零為起始的索引。 設定為 -1 以便從控制項的開頭進行搜尋。

傳回

找到的第一個項目之以零為起始的索引;如果找不到相符項,則傳回 -1;如果 s 參數指定 Empty,則傳回 0。

例外狀況

startIndex 小於 -1。

-或-

startIndex 大於集合中的最後索引。

備註

這個方法執行的搜尋不會區分大小寫。 參數 s 是子字串,可與下拉式方塊清單中的專案相關聯的文字進行比較。 搜尋會從文字開頭開始執行部分比對,並傳回清單中符合指定子字串的第一個專案。 然後,您可以執行工作,例如使用 Remove 方法來移除包含搜尋文字的專案,或變更專案的文字。 這個方法通常會在使用未指定起始索引的這個方法版本進行呼叫之後使用。 在清單中找到初始專案之後,這個方法通常用來尋找搜尋文字的進一步實例,方法是在搜尋文字的第一個找到實例之後,于專案的 參數中 startIndex 指定索引位置。 如果您想要執行搜尋完全相符的字組,而不是部分比對,請使用 FindStringExact 方法。

適用於