共用方式為


ComboBox.FindStringExact 方法

定義

尋找完全符合指定字串的項目。

多載

FindStringExact(String)

尋找下拉式方塊中第一個完全符合指定字串的項目。

FindStringExact(String, Int32)

尋找在指定索引之後第一個符合指定字串的項目。

FindStringExact(String)

尋找下拉式方塊中第一個完全符合指定字串的項目。

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

參數

s
String

要搜尋的 String

傳回

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

範例

下列程式碼範例示範如何藉由設定 MaxDropDownItems 和 屬性,以及使用 FindStringExact 方法來搜尋 ComboBox ,初始化 ComboBoxDropDownStyle 控制項。 它也會顯示處理 SelectedIndexChanged 事件。

#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System::Windows::Forms;
using namespace System;

public ref class Form1: public System::Windows::Forms::Form
{
public:
   Form1() : Form()
   {
      InitializeComboBox();
      InitializeTextBox();
      this->Label1 = gcnew System::Windows::Forms::Label;
      this->SuspendLayout();
      this->Label1->Location = System::Drawing::Point( 8, 24 );
      this->Label1->Name = "Label1";
      this->Label1->Size = System::Drawing::Size( 120, 32 );
      this->Label1->TabIndex = 1;
      this->Label1->Text = "Use drop-down to choose a name:";
      this->Label1->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Controls->Add( this->Label1 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );
   }

internal:
   System::Windows::Forms::Label ^ Label1;

   // Declare and initialize the text box.
internal:
   // This text box text will be update programmatically. The user is not 
   // allowed to update it, so the ReadOnly property is set to true.
   System::Windows::Forms::TextBox^ TextBox1;

private:
   void InitializeTextBox()
   {
      this->TextBox1 = gcnew System::Windows::Forms::TextBox;
      this->TextBox1->ScrollBars = ScrollBars::Vertical;
      this->TextBox1->Location = System::Drawing::Point( 64, 128 );
      this->TextBox1->Multiline = true;
      this->TextBox1->Name = "TextBox1";
      this->TextBox1->ReadOnly = true;
      this->TextBox1->Size = System::Drawing::Size( 184, 120 );
      this->TextBox1->TabIndex = 4;
      this->TextBox1->Text = "Employee and Number of Awards:";
      this->Controls->Add( this->TextBox1 );
   }

   // Declare comboBox1 as a ComboBox.
internal:
   System::Windows::Forms::ComboBox^ ComboBox1;

private:
   // 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.
   void InitializeComboBox()
   {
      this->ComboBox1 = gcnew System::Windows::Forms::ComboBox;
      array<String^>^ employees = {"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 = System::Drawing::Point( 136, 32 );
      this->ComboBox1->IntegralHeight = false;
      this->ComboBox1->MaxDropDownItems = 5;
      this->ComboBox1->DropDownStyle = ComboBoxStyle::DropDownList;
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = 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 +=
         gcnew System::EventHandler( this, &Form1::ComboBox1_SelectedIndexChanged );
   }

private:
   // 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.
   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 );
      }

      TextBox1->Text = TextBox1->Text + "\r\n" + selectedEmployee + ": " +
         count;
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}
using System.Windows.Forms;

public class Form1:
    System.Windows.Forms.Form

{
    public Form1() : base()
    {        
        InitializeComboBox();
        InitializeTextBox();
        this.Label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.Label1.Location = new System.Drawing.Point(8, 24);
        this.Label1.Name = "Label1";
        this.Label1.Size = new System.Drawing.Size(120, 32);
        this.Label1.TabIndex = 1;
        this.Label1.Text = "Use drop-down to choose a name:";
        this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.Label1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }

    internal System.Windows.Forms.Label Label1;


    // Declare and initialize the text box.
    // This text box text will be update programmatically. The user is not 
    // allowed to update it, so the ReadOnly property is set to true.
    internal System.Windows.Forms.TextBox TextBox1;

    private void InitializeTextBox()
    {
        this.TextBox1 = new System.Windows.Forms.TextBox();
        this.TextBox1.ScrollBars = ScrollBars.Vertical;
        this.TextBox1.Location = new System.Drawing.Point(64, 128);
        this.TextBox1.Multiline = true;
        this.TextBox1.Name = "TextBox1";
        this.TextBox1.ReadOnly = true;
        this.TextBox1.Size = new System.Drawing.Size(184, 120);
        this.TextBox1.TabIndex = 4;
        this.TextBox1.Text = "Employee and Number of Awards:";
        this.Controls.Add(this.TextBox1);
    }

    // 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.IntegralHeight = false;
        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);
    }

    // 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;
    }
}
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComboBox()
        InitializeTextBox()
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        Me.Label1.Location = New System.Drawing.Point(8, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 32)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Use drop-down to choose a name:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub

    Shared Sub Main()
        Application.Run(New Form1)
    End Sub

   
    Friend WithEvents Label1 As System.Windows.Forms.Label
    
    
    

    ' Declare and initialize the text box.
    ' This text box text will be update programmatically. The user is not 
    ' allowed to update it, so the ReadOnly property is set to true.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    Private Sub InitializeTextBox()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox1.ScrollBars = ScrollBars.Vertical
        Me.TextBox1.Location = New System.Drawing.Point(64, 128)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(184, 120)
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = "Employee and Number of Awards:"
        Me.Controls.Add(Me.TextBox1)
    End Sub


    ' 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.IntegralHeight = False
        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



    ' 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

End Class

備註

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

適用於

FindStringExact(String, Int32)

尋找在指定索引之後第一個符合指定字串的項目。

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

參數

s
String

要搜尋的 String

startIndex
Int32

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

傳回

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

例外狀況

startIndex 小於 -1。

-或-

startIndex 等於集合中的最後一個索引。

範例

下列程式碼範例示範如何藉由設定 MaxDropDownItems 和 屬性,以及使用 FindStringExact 方法來搜尋 ComboBox ,初始化 ComboBoxDropDownStyle 控制項。 它也會顯示處理 SelectedIndexChanged 事件。

#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System::Windows::Forms;
using namespace System;

public ref class Form1: public System::Windows::Forms::Form
{
public:
   Form1() : Form()
   {
      InitializeComboBox();
      InitializeTextBox();
      this->Label1 = gcnew System::Windows::Forms::Label;
      this->SuspendLayout();
      this->Label1->Location = System::Drawing::Point( 8, 24 );
      this->Label1->Name = "Label1";
      this->Label1->Size = System::Drawing::Size( 120, 32 );
      this->Label1->TabIndex = 1;
      this->Label1->Text = "Use drop-down to choose a name:";
      this->Label1->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Controls->Add( this->Label1 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );
   }

internal:
   System::Windows::Forms::Label ^ Label1;

   // Declare and initialize the text box.
internal:
   // This text box text will be update programmatically. The user is not 
   // allowed to update it, so the ReadOnly property is set to true.
   System::Windows::Forms::TextBox^ TextBox1;

private:
   void InitializeTextBox()
   {
      this->TextBox1 = gcnew System::Windows::Forms::TextBox;
      this->TextBox1->ScrollBars = ScrollBars::Vertical;
      this->TextBox1->Location = System::Drawing::Point( 64, 128 );
      this->TextBox1->Multiline = true;
      this->TextBox1->Name = "TextBox1";
      this->TextBox1->ReadOnly = true;
      this->TextBox1->Size = System::Drawing::Size( 184, 120 );
      this->TextBox1->TabIndex = 4;
      this->TextBox1->Text = "Employee and Number of Awards:";
      this->Controls->Add( this->TextBox1 );
   }

   // Declare comboBox1 as a ComboBox.
internal:
   System::Windows::Forms::ComboBox^ ComboBox1;

private:
   // 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.
   void InitializeComboBox()
   {
      this->ComboBox1 = gcnew System::Windows::Forms::ComboBox;
      array<String^>^ employees = {"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 = System::Drawing::Point( 136, 32 );
      this->ComboBox1->IntegralHeight = false;
      this->ComboBox1->MaxDropDownItems = 5;
      this->ComboBox1->DropDownStyle = ComboBoxStyle::DropDownList;
      this->ComboBox1->Name = "ComboBox1";
      this->ComboBox1->Size = 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 +=
         gcnew System::EventHandler( this, &Form1::ComboBox1_SelectedIndexChanged );
   }

private:
   // 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.
   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 );
      }

      TextBox1->Text = TextBox1->Text + "\r\n" + selectedEmployee + ": " +
         count;
   }
};

int main()
{
   Application::Run( gcnew Form1 );
}
using System.Windows.Forms;

public class Form1:
    System.Windows.Forms.Form

{
    public Form1() : base()
    {        
        InitializeComboBox();
        InitializeTextBox();
        this.Label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.Label1.Location = new System.Drawing.Point(8, 24);
        this.Label1.Name = "Label1";
        this.Label1.Size = new System.Drawing.Size(120, 32);
        this.Label1.TabIndex = 1;
        this.Label1.Text = "Use drop-down to choose a name:";
        this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.Label1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }

    internal System.Windows.Forms.Label Label1;


    // Declare and initialize the text box.
    // This text box text will be update programmatically. The user is not 
    // allowed to update it, so the ReadOnly property is set to true.
    internal System.Windows.Forms.TextBox TextBox1;

    private void InitializeTextBox()
    {
        this.TextBox1 = new System.Windows.Forms.TextBox();
        this.TextBox1.ScrollBars = ScrollBars.Vertical;
        this.TextBox1.Location = new System.Drawing.Point(64, 128);
        this.TextBox1.Multiline = true;
        this.TextBox1.Name = "TextBox1";
        this.TextBox1.ReadOnly = true;
        this.TextBox1.Size = new System.Drawing.Size(184, 120);
        this.TextBox1.TabIndex = 4;
        this.TextBox1.Text = "Employee and Number of Awards:";
        this.Controls.Add(this.TextBox1);
    }

    // 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.IntegralHeight = false;
        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);
    }

    // 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;
    }
}
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComboBox()
        InitializeTextBox()
        Me.Label1 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        Me.Label1.Location = New System.Drawing.Point(8, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 32)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Use drop-down to choose a name:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub

    Shared Sub Main()
        Application.Run(New Form1)
    End Sub

   
    Friend WithEvents Label1 As System.Windows.Forms.Label
    
    
    

    ' Declare and initialize the text box.
    ' This text box text will be update programmatically. The user is not 
    ' allowed to update it, so the ReadOnly property is set to true.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    Private Sub InitializeTextBox()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox1.ScrollBars = ScrollBars.Vertical
        Me.TextBox1.Location = New System.Drawing.Point(64, 128)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(184, 120)
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = "Employee and Number of Awards:"
        Me.Controls.Add(Me.TextBox1)
    End Sub


    ' 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.IntegralHeight = False
        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



    ' 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

End Class

備註

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

此方法執行的搜尋是迴圈的。 雖然搜尋會在 參數之後 startIndex 的下一個索引開始,但達到集合結尾時,搜尋會在 0 重新開機。 不過請注意,如果 startIndex 參數本身等於集合中的最後一個索引,則會擲回例外狀況。

適用於