ComboBox.FindStringExact ComboBox.FindStringExact ComboBox.FindStringExact ComboBox.FindStringExact Method

定義

指定した文字列と厳密に一致する項目を検索します。Finds the item that exactly matches the specified string.

オーバーロード

FindStringExact(String) FindStringExact(String) FindStringExact(String) FindStringExact(String)

コンボ ボックス内で、指定した文字列と一致する最初の項目を検索します。Finds the first item in the combo box that matches the specified string.

FindStringExact(String, Int32) FindStringExact(String, Int32) FindStringExact(String, Int32) FindStringExact(String, Int32)

指定したインデックスの後に出現する、指定した文字列と一致する最初の項目を検索します。Finds the first item after the specified index that matches the specified string.

FindStringExact(String) FindStringExact(String) FindStringExact(String) FindStringExact(String)

コンボ ボックス内で、指定した文字列と一致する最初の項目を検索します。Finds the first item in the combo box that matches the specified string.

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

パラメーター

s
String String String String

検索対象の StringThe String to search for.

戻り値

最初に見つかった項目のインデックス (0 から始まる)。一致する項目が見つからない場合は -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.

次のコード例は、 ComboBox DropDownStyle MaxDropDownItems FindStringExactプロパティとプロパティを設定し、メソッドを使用してを検索することによって、コントロールを初期化する方法を示しています。ComboBoxThe following code example demonstrates how to initialize a ComboBox control by setting the MaxDropDownItems and DropDownStyle properties and using the FindStringExact methods to search the ComboBox. また、イベントの処理SelectedIndexChangedについても説明します。It also shows handling the SelectedIndexChanged event.

#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

注釈

このメソッドで実行される検索では、大文字と小文字は区別されません。The search performed by this method is not case-sensitive. sパラメーターは、コンボボックスリスト内の項目に関連付けられたテキストと比較する文字列です。The s parameter is a string to compare against the text associated with the items in the combo box list. 検索では、テキストの先頭から一致するものを検索し、指定した部分文字列と一致するリスト内の最初の項目を返します。The search looks for a 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. 指定したテキストが見つかったら、内ComboBoxのテキストの他のインスタンスを検索するには、の開始インデックスComboBoxを指定するためのFindStringExactパラメーターを提供するバージョンのメソッドを使用する必要があります。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 FindStringExact method that provides a parameter for specifying a starting index within the ComboBox. 単語の完全一致ではなく部分的な検索を実行する場合は、 FindStringメソッドを使用します。If you want to perform partial word search instead of an exact word match, use the FindString method.

FindStringExact(String, Int32) FindStringExact(String, Int32) FindStringExact(String, Int32) FindStringExact(String, Int32)

指定したインデックスの後に出現する、指定した文字列と一致する最初の項目を検索します。Finds the first item after the specified index that matches the specified string.

public:
 int FindStringExact(System::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 String String

検索対象の StringThe String to search for.

startIndex
Int32 Int32 Int32 Int32

最初の検索対象項目の前にある項目の 0 から始まるインデックス番号。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.

戻り値

最初に見つかった項目のインデックス (0 から始まる)。一致する項目が見つからない場合は -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 equal to the last index in the collection.

次のコード例は、 ComboBox DropDownStyle MaxDropDownItems FindStringExactプロパティとプロパティを設定し、メソッドを使用してを検索することによって、コントロールを初期化する方法を示しています。ComboBoxThe following code example demonstrates how to initialize a ComboBox control by setting the MaxDropDownItems and DropDownStyle properties and using the FindStringExact methods to search the ComboBox. また、イベントの処理SelectedIndexChangedについても説明します。It also shows handling the SelectedIndexChanged event.

#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

注釈

このメソッドで実行される検索では、大文字と小文字は区別されません。The search performed by this method is not case-sensitive. sパラメーターは、コンボボックスリスト内の項目に関連付けられたテキストと比較する文字列です。The s parameter is a string to compare against the text associated with the items in the combo box list. 検索では、テキストの先頭から一致するものを検索し、指定した部分文字列と一致するリスト内の最初の項目を返します。The search looks for a 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. 単語の完全一致ではなく部分的な検索を実行する場合は、 FindStringメソッドを使用します。If you want to perform partial word search instead of an exact word match, use the FindString method.

このメソッドで実行される検索は循環しています。The search performed by this method is circular. 検索は、パラメーターのstartIndex後の次のインデックスから開始されますが、コレクションの末尾に到達すると、検索は0で再開されます。Although the search starts at the next index after the startIndex parameter, the search restarts at 0 when the end of the collection is reached. ただし、 startIndexパラメーター自体がコレクション内の最後のインデックスと等しい場合は、例外がスローされることに注意してください。However, note that if the startIndex parameter itself is equal to the last index in the collection, an exception is thrown.

適用対象