次の方法で共有


ComboBox.FindStringExact メソッド (String, Int32)

指定したインデックスの後に出現する、指定した文字列と一致する最初の項目を検索します。

Overloads Public Function FindStringExact( _
   ByVal s As String, _   ByVal startIndex As Integer _) As Integer
[C#]
public int FindStringExact(strings,intstartIndex);
[C++]
public: int FindStringExact(String* s,intstartIndex);
[JScript]
public function FindStringExact(
   s : String,startIndex : int) : int;

パラメータ

  • s
    検索対象の String
  • startIndex
    最初の検索対象項目の前にある項目の 0 から始まるインデックス番号。コントロールの先頭から検索する場合は -1 を設定します。

戻り値

最初に見つかった項目の 0 から始まるインデックス番号。一致する項目が見つからない場合は、-1 を返します。

例外

例外の種類 条件
ArgumentOutOfRangeException startIndex が -1 未満です。

または

startIndex がコレクション内の最後のインデックスと等しい値です。

解説

このメソッドで実行される検索では、大文字と小文字は区別されません。 s パラメータは、コンボ ボックスのリストの項目に関連付けられたテキストの比較対象となる文字列です。検索はテキストの先頭から開始され、指定した部分文字列に一致する、リスト内の最初の項目が返されます。その後で、 Remove メソッドを使用した検索文字列を含む項目の削除、項目のテキストの変更などのタスクを実行できます。このメソッドは通常、開始インデックスを指定しない形式でこのメソッドを呼び出した後に使用されます。通常は、検索文字列に一致する最初の項目が見つかった後で、同じ文字列に一致する別の項目をさらに検索するために、最初に見つかった項目の後ろの項目のインデックス位置を startIndex パラメータに指定して、このメソッドを使用します。厳密な単語の一致ではなく単語の一部で検索を実行するには、 FindString メソッドを使用します。

このメソッドで実行される検索は循環検索です。検索は startIndex パラメータの後の次のインデックスから開始されますが、コレクションの末尾に到達したら、その検索は 0 から再開始されます。ただし、 startIndex パラメータ自体がコレクション内の最後のインデックスと同じ場合は、例外がスローされます。

使用例

FindStringExact メソッドを使用するコード例を次に示します。この例を実行するには、TextBox1 という名前の TextBox オブジェクトが配置されているフォームに次のコードを貼り付けて、フォームのコンストラクタまたは Load メソッドから InitializeComboBox メソッドを呼び出します。

[SampleID='System.Windows.Forms.ComboBoxFindString' SnippetID='1,2']
--------- Languages displayed= cs, vb ---------
--------- cs ---------
--------- Snippet 1 ---------
    // Declare comboBox1 as a ComboBox.
    internal System.Windows.Forms.ComboBox ComboBox1;
    
    // This method initializes the combo box, adding a large string array
    // but limiting the drop-down size to six rows so the combo box doesn't 
    // cover other controls when it expands.
    private void InitializeComboBox()
    {
        this.ComboBox1 = new System.Windows.Forms.ComboBox();
        string[] employees = new string[]{"Hamilton, David", "Hensien, Kari",
                "Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 
                "Hanson, Mark", "Harnpadoungsataya, Sariya", 
                "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", 
                "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", 
                "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", 
                "Henshaw, Jeff D.", "Hensien, Kari", "Harris, Keith", 
                "Henshaw, Jeff D.", "Hensien, Kari", "Hasselberg, Jonas",
                "Harrington, Mark", "Hedlund, Magnus", "Hay, Jeff", 
                "Heidepriem, Brandon D."};
        ComboBox1.Items.AddRange(employees);
        this.ComboBox1.Location = new System.Drawing.Point(136, 32);
        this.ComboBox1.MaxDropDownItems = 5;
        this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        this.ComboBox1.Name = "ComboBox1";
        this.ComboBox1.Size = new System.Drawing.Size(136, 81);
        this.ComboBox1.TabIndex = 0;
        this.Controls.Add(this.ComboBox1);
        
        // Associate the event-handling method with the 
        // SelectedIndexChanged event.
        this.ComboBox1.SelectedIndexChanged += 
            new System.EventHandler(ComboBox1_SelectedIndexChanged);
    }
--------- Snippet 2 ---------
    // This method is called when the user changes his or her selection.
    // It searches for all occurrences of the selected employee's
    // name in the Items array and adds the employee's name and 
    // the number of occurrences to TextBox1.Text.
    // CAUTION   This code exposes a known bug: If the index passed to the 
    // FindStringExact(searchString, index) method is the last index 
    // of the array, the code throws an exception.
    private void ComboBox1_SelectedIndexChanged(object sender, 
        System.EventArgs e)
    {
        ComboBox comboBox = (ComboBox) sender;
        // Save the selected employee's name, because we will remove
        // the employee's name from the list.
        string selectedEmployee = (string) ComboBox1.SelectedItem;
        int count = 0;
        int resultIndex = -1;
        // Call the FindStringExact method to find the first 
        // occurrence in the list.
        resultIndex = ComboBox1.FindStringExact(selectedEmployee);
        // Remove the name as it is found, and increment the found count. 
        // Then call the FindStringExact method again, passing in the 
        // index of the current found item so the search starts there 
        // instead of at the beginning of the list.
        while (resultIndex!=-1)
        {
            ComboBox1.Items.RemoveAt(resultIndex);
            count += 1;
            resultIndex = ComboBox1.FindStringExact(selectedEmployee, 
                resultIndex);
        }
        // Update the text in Textbox1.
        TextBox1.Text = TextBox1.Text+ "\r\n" + selectedEmployee + ": "
            + count;
    }
--------- vb ---------
--------- Snippet 1 ---------
    ' Declare comboBox1 as a ComboBox.
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    ' This method initializes the combo box, adding a large string 
    ' array but limiting the drop-down size to six rows so the combo box
    ' doesn't cover other controls when it expands.
    Private Sub InitializeComboBox()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Dim employees() As String = New String() {"Hamilton, David", _
            "Hensien, Kari", "Hammond, Maria", "Harris, Keith", _
            "Henshaw, Jeff D.", "Hanson, Mark", "Harnpadoungsataya, Sariya", _
            "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", _
            "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", _
            "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Harris, Keith", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Hasselberg, Jonas", "Harrington, Mark", _
            "Hedlund, Magnus", "Hay, Jeff", "Heidepriem, Brandon D."}
        ComboBox1.Items.AddRange(employees)
        Me.ComboBox1.Location = New System.Drawing.Point(136, 32)
        Me.ComboBox1.MaxDropDownItems = 5
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(136, 81)
        Me.ComboBox1.TabIndex = 0
        Me.Controls.Add(Me.ComboBox1)
    End Sub
--------- Snippet 2 ---------
    ' This method is called when the user changes his or her selection.
    ' It searches for all occurrences of the selected employee's
    ' name in the Items array and adds the employee's name and 
    ' the number of occurrences to TextBox1.Text.
    ' CAUTION   This code exposes a known bug: If the index passed to the 
    ' FindStringExact(searchString, index) method is the last index 
    ' of the array, the code throws an exception.
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim comboBox As comboBox = CType(sender, comboBox)
        ' Save the selected employee's name, because we will remove
        ' the employee's name from the list.
        Dim selectedEmployee = CType(ComboBox1.SelectedItem, String)
        Dim count As Integer = 0
        Dim resultIndex As Integer = -1
        ' Call the FindStringExact method to find the first 
        ' occurrence in the list.
        resultIndex = ComboBox1.FindStringExact(ComboBox1.SelectedItem)
        ' Remove the name as it is found, and increment the found count. 
        ' Then call the FindStringExact method again, passing in the index of the
        ' current found item so the search starts there instead of 
        ' at the beginning of the list.
        While (resultIndex <> -1)
            ComboBox1.Items.RemoveAt(resultIndex)
            count += 1
            resultIndex = ComboBox1.FindStringExact _
            (selectedEmployee, resultIndex)
        End While
        ' Update the text in Textbox1.
        TextBox1.Text = TextBox1.Text & Microsoft.VisualBasic.vbCrLf _
            & selectedEmployee & ": " & count
   End Sub

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

ComboBox クラス | ComboBox メンバ | System.Windows.Forms 名前空間 | ComboBox.FindStringExact オーバーロードの一覧