ComboBox.ObjectCollection.RemoveAt メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ComboBox.ObjectCollection.RemoveAt メソッドの意味・解説 

ComboBox.ObjectCollection.RemoveAt メソッド

ComboBox 内の指定したインデックス位置の項目を削除します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

value パラメータが 0 未満でした。

または

value パラメータコレクション内の項目数上の値でした。

使用例使用例

MaxDropDownItems プロパティと DropDownStyle プロパティ設定しComboBox検索する FindStringExact メソッドComboBox から項目を削除する RemoveAt メソッド使用してComboBox コントロール初期化する方法次のコード例示します。この例はまた、SelectedIndexChanged イベント処理方法示してます。この例を実行するには、TextBox1 という名前の TextBox オブジェクト配置されているフォーム次のコード貼り付けてフォームコンストラクタまたは Load メソッドから InitializeComboBox メソッド呼び出します。

    ' 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


<br /><span space="preserve">...</span><br
 />    ' 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
    // 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);
    }
<br /><span space="preserve">...</span><br /> 
   // 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;
    }
   // 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->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
 );
   }
<br /><span space="preserve">...</span><br />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;
   }
    // Declare comboBox1 as a ComboBox.
    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.get_Items().AddRange(employees);
        this.comboBox1.set_Location(new System.Drawing.Point(136,
 32));
        this.comboBox1.set_MaxDropDownItems(5);
        this.comboBox1.set_DropDownStyle(ComboBoxStyle.DropDownList);
        this.comboBox1.set_Name("comboBox1");
        this.comboBox1.set_Size(new System.Drawing.Size(136,
 81));
        this.comboBox1.set_TabIndex(0);
        this.get_Controls().Add(this.comboBox1);
        // Associate the event-handling method with the 
        // SelectedIndexChanged event.
        this.comboBox1.add_SelectedIndexChanged(new
 System.EventHandler(
            comboBox1_SelectedIndexChanged));
    } //InitializeComboBox
<br /><span space="preserve">...</span><br /> 
   // 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.get_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.get_Items().RemoveAt(resultIndex);
            count += 1;
            resultIndex = comboBox1.FindStringExact(selectedEmployee, 
                resultIndex);
        }
        // Update the text in textBox1.
        textBox1.set_Text(textBox1.get_Text() + "\r\n" + selectedEmployee
 
            + ": " + count);
    } //comboBox1_SelectedIndexChanged
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ComboBox.ObjectCollection クラス
ComboBox.ObjectCollection メンバ
System.Windows.Forms 名前空間
IList



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ComboBox.ObjectCollection.RemoveAt メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ComboBox.ObjectCollection.RemoveAt メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS