次の方法で共有


CheckedListBox クラス

各項目の左側にチェック ボックスが表示される ListBox を表示します。

この型のすべてのメンバの一覧については、CheckedListBox メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ListControl
               System.Windows.Forms.ListBox
                  System.Windows.Forms.CheckedListBox

Public Class CheckedListBox
   Inherits ListBox
[C#]
public class CheckedListBox : ListBox
[C++]
public __gc class CheckedListBox : public ListBox
[JScript]
public class CheckedListBox extends ListBox

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

このコントロールは、キーボード、またはコントロールの右側にあるスクロール バーを使用して移動できる項目のリストを示します。ユーザーは 1 つ以上の項目にチェック マークを付けることができ、 CheckedListBox.CheckedItemCollection および CheckedListBox.CheckedIndexCollection を使用して、チェックした項目の間を移動できます。

実行時にオブジェクトをリストに追加するには、オブジェクト参照の配列を AddRange メソッドを使用して割り当てます。各オブジェクトの既定の文字列値がリストに表示されます。 Add メソッドを使用して、個別の項目をリストに追加できます。

CheckedListBox オブジェクトは、 CheckState 列挙体を使用して、3 つの状態 (CheckedIndeterminate 、および Unchecked) をサポートします。 Indeterminate の状態は、 CheckedListBox のユーザー インターフェイスに設定のための機構が用意されていないため、コードで設定する必要があります。

UseTabStopstrue の場合、 CheckedListBox は項目のテキスト内のタブ文字を認識して展開し、列を作成します。ただし、タブ ストップは存在し、変更はできません。

CheckedListBox クラスは、次の 3 つのインデックス付きコレクションをサポートします。

コレクション クラスのカプセル化
CheckedListBox コントロールに格納されているすべての項目。 CheckedListBox.ObjectCollection
チェックされた項目 (不確定な状態の項目を含む) であり、 CheckedListBox コントロールに格納されている項目のサブセット。 CheckedListBox.CheckedItemCollection
チェックされているインデックスであり、項目のコレクションへのインデックスのサブセット。これらのインデックスで、チェックされている状態または不確定な状態の項目を指定します。 CheckedListBox.CheckedIndexCollection

CheckedListBox クラスがサポートする 3 つのインデックス付きコレクションの例を次の 3 つの表に示します。

1 番目の表では、コントロールの項目 (コントロールに格納されているすべての項目) のインデックス付きコレクションの例を示しています。

インデックス 項目 チェックの状態
0 オブジェクト 1 Unchecked
1 オブジェクト 2 Checked
2 オブジェクト 3 Unchecked
3 オブジェクト 4 Indeterminate
4 オブジェクト 5 Checked

2 番目の表では、チェックされている項目のインデックス付きコレクションの例を示しています。

インデックス 項目
0 オブジェクト 2
1 オブジェクト 4
2 オブジェクト 5

3 番目の表では、チェックされている項目のインデックスのインデックス付きコレクションの例を示しています。

インデックス 項目のインデックス
0 1
1 3
2 4

使用例

[Visual Basic, C#, C++] CheckedListBox のメソッド、プロパティ、およびコレクションの使い方の例を次に示します。これは完成されたサンプルであるため、プロジェクトにコピーすると実行できます。項目をチェックしたり解除したり、テキスト ボックスを使用して項目を追加したりできます。保存ボタンをクリックすると、チェックした項目がクリアされます。

 
Option Explicit
Option Strict

Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.IO

Namespace WindowsApplication1
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private WithEvents checkedListBox1 As System.Windows.Forms.CheckedListBox
        Private WithEvents textBox1 As System.Windows.Forms.TextBox
        Private WithEvents button1 As System.Windows.Forms.Button
        Private WithEvents button2 As System.Windows.Forms.Button
        Private WithEvents listBox1 As System.Windows.Forms.ListBox
        Private WithEvents button3 As System.Windows.Forms.Button
        Private components As System.ComponentModel.Container
        
        
        Public Sub New()
            InitializeComponent()
            
            ' Sets up the initial objects in the CheckedListBox.
            Dim myFruit As String() =  {"Apples", "Oranges", "Tomato"}
            checkedListBox1.Items.AddRange(myFruit)
            
            ' Changes the selection mode from double-click to single click.
            checkedListBox1.CheckOnClick = True
        End Sub 'New
        
        
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
         
        Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container()
            Me.textBox1 = New System.Windows.Forms.TextBox()
            Me.checkedListBox1 = New System.Windows.Forms.CheckedListBox()
            Me.listBox1 = New System.Windows.Forms.ListBox()
            Me.button1 = New System.Windows.Forms.Button()
            Me.button2 = New System.Windows.Forms.Button()
            Me.button3 = New System.Windows.Forms.Button()
            Me.textBox1.Location = New System.Drawing.Point(144, 64)
            Me.textBox1.Size = New System.Drawing.Size(128, 20)
            Me.textBox1.TabIndex = 1
            Me.checkedListBox1.Location = New System.Drawing.Point(16, 64)
            Me.checkedListBox1.Size = New System.Drawing.Size(120, 184)
            Me.checkedListBox1.TabIndex = 0
            Me.listBox1.Location = New System.Drawing.Point(408, 64)
            Me.listBox1.Size = New System.Drawing.Size(128, 186)
            Me.listBox1.TabIndex = 3
            Me.button1.Enabled = False
            Me.button1.Location = New System.Drawing.Point(144, 104)
            Me.button1.Size = New System.Drawing.Size(104, 32)
            Me.button1.TabIndex = 2
            Me.button1.Text = "Add Fruit"
            Me.button2.Enabled = False
            Me.button2.Location = New System.Drawing.Point(288, 64)
            Me.button2.Size = New System.Drawing.Size(104, 32)
            Me.button2.TabIndex = 2
            Me.button2.Text = "Show Order"
            Me.button3.Enabled = False
            Me.button3.Location = New System.Drawing.Point(288, 104)
            Me.button3.Size = New System.Drawing.Size(104, 32)
            Me.button3.TabIndex = 2
            Me.button3.Text = "Save Order"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(563, 273)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.listBox1, Me.button3, Me.button2, Me.button1, Me.textBox1, Me.checkedListBox1})
            Me.Text = "Fruit Order"
        End Sub 'InitializeComponent
        
        <STAThread()> _
        Public Shared Sub Main()
            Application.Run(New Form1())
        End Sub 'Main
        
        
        ' Adds the string if the text box has data in it.
        Private Sub button1_Click(sender As Object, _
                e As System.EventArgs) Handles button1.Click
            If textBox1.Text <> "" Then
                If checkedListBox1.CheckedItems.Contains(textBox1.Text) = False Then
                    checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked)
                End If
                textBox1.Text = ""
            End If
        End Sub 'button1_Click
         
        ' Activates or deactivates the Add button.
        Private Sub textBox1_TextChanged(sender As Object, _
                e As System.EventArgs) Handles textBox1.TextChanged
            If textBox1.Text = "" Then
                button1.Enabled = False
            Else
                button1.Enabled = True
            End If
        End Sub 'textBox1_TextChanged
         
        
        ' Moves the checked items from the CheckedListBox to the listBox.
        Private Sub button2_Click(sender As Object, _
                e As System.EventArgs) Handles button2.Click
            listBox1.Items.Clear()
            button3.Enabled = False
            Dim i As Integer
            For i = 0 To checkedListBox1.CheckedItems.Count - 1
                listBox1.Items.Add(checkedListBox1.CheckedItems(i))
            Next i
            If listBox1.Items.Count > 0 Then
                button3.Enabled = True
            End If 
        End Sub 'button2_Click
        
        ' Activates the move button if there are checked items.
        Private Sub checkedListBox1_ItemCheck(sender As Object, _
                e As ItemCheckEventArgs) Handles checkedListBox1.ItemCheck
            If e.NewValue = CheckState.Unchecked Then
                If checkedListBox1.CheckedItems.Count = 1 Then
                    button2.Enabled = False
                End If
            Else
                button2.Enabled = True
            End If
        End Sub 'checkedListBox1_ItemCheck
        
        
        ' Saves the items to a file.
        Private Sub button3_Click(sender As Object, _
                e As System.EventArgs) Handles button3.Click
            ' Insert code to save a file.
            listBox1.Items.Clear()
            Dim myEnumerator As IEnumerator
            myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator()
            Dim y As Integer
            While myEnumerator.MoveNext() <> False
                y = CInt(myEnumerator.Current)
                checkedListBox1.SetItemChecked(y, False)
            End While
            button3.Enabled = False
        End Sub 'button3_Click
    End Class 'Form1
End Namespace 'WindowsApplication1


[C#] 
namespace WindowsApplication1
{
   using System;
   using System.Drawing;
   using System.Collections;
   using System.ComponentModel;
   using System.Windows.Forms;
   using System.Data;
   using System.IO ;

   public class Form1 : System.Windows.Forms.Form
   {
      private System.Windows.Forms.CheckedListBox checkedListBox1;
      private System.Windows.Forms.TextBox textBox1;
      private System.Windows.Forms.Button button1;
      private System.Windows.Forms.Button button2;
      private System.Windows.Forms.ListBox listBox1;
      private System.Windows.Forms.Button button3;
        private System.ComponentModel.Container components;
      
      public Form1()
      {
            InitializeComponent();

         // Sets up the initial objects in the CheckedListBox.
            string[] myFruit = {"Apples", "Oranges","Tomato"};
         checkedListBox1.Items.AddRange(myFruit);

            // Changes the selection mode from double-click to single click.
         checkedListBox1.CheckOnClick = true;
      }

      protected override void Dispose( bool disposing )
      {
        if( disposing )
        {
            if (components != null) 
            {
              components.Dispose();
            }
        }
        base.Dispose( disposing );
      }

      private void InitializeComponent()
      {
         this.components = new System.ComponentModel.Container();
         this.textBox1 = new System.Windows.Forms.TextBox();
         this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
         this.listBox1 = new System.Windows.Forms.ListBox();
         this.button1 = new System.Windows.Forms.Button();
         this.button2 = new System.Windows.Forms.Button();
         this.button3 = new System.Windows.Forms.Button();
         this.textBox1.Location = new System.Drawing.Point(144, 64);
         this.textBox1.Size = new System.Drawing.Size(128, 20);
         this.textBox1.TabIndex = 1;
         this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
         this.checkedListBox1.Location = new System.Drawing.Point(16, 64);
         this.checkedListBox1.Size = new System.Drawing.Size(120, 184);
         this.checkedListBox1.TabIndex = 0;
         this.checkedListBox1.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
         this.listBox1.Location = new System.Drawing.Point(408, 64);
         this.listBox1.Size = new System.Drawing.Size(128, 186);
         this.listBox1.TabIndex = 3;
         this.button1.Enabled = false;
         this.button1.Location = new System.Drawing.Point(144, 104);
         this.button1.Size = new System.Drawing.Size(104, 32);
         this.button1.TabIndex = 2;
         this.button1.Text = "Add Fruit";
         this.button1.Click += new System.EventHandler(this.button1_Click);
         this.button2.Enabled = false;
         this.button2.Location = new System.Drawing.Point(288, 64);
         this.button2.Size = new System.Drawing.Size(104, 32);
         this.button2.TabIndex = 2;
         this.button2.Text = "Show Order";
         this.button2.Click += new System.EventHandler(this.button2_Click);
         this.button3.Enabled = false;
         this.button3.Location = new System.Drawing.Point(288, 104);
         this.button3.Size = new System.Drawing.Size(104, 32);
         this.button3.TabIndex = 2;
         this.button3.Text = "Save Order";
         this.button3.Click += new System.EventHandler(this.button3_Click);
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(563, 273);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {this.listBox1,
                                                        this.button3,
                                                        this.button2,
                                                        this.button1,
                                                        this.textBox1,
                                                        this.checkedListBox1});
         this.Text = "Fruit Order";
      }

      [STAThread]
      public static void Main(string[] args) 
      {
         Application.Run(new Form1());
      }

      // Adds the string if the text box has data in it.
      private void button1_Click(object sender, System.EventArgs e)
      {
         if(textBox1.Text != "")
         {
            if(checkedListBox1.CheckedItems.Contains(textBox1.Text)== false)
               checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
            textBox1.Text = "";
         }

      }
      // Activates or deactivates the Add button.
      private void textBox1_TextChanged(object sender, System.EventArgs e)
      {
         if (textBox1.Text == "")
         {
            button1.Enabled = false;
         }
         else
         {
            button1.Enabled = true;
         }
            
        }

      // Moves the checked items from the CheckedListBox to the listBox.
      private void button2_Click(object sender, System.EventArgs e)
      {
         listBox1.Items.Clear();
         button3.Enabled=false;
         for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
         {
            listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
         }
         if (listBox1.Items.Count>0)
            button3.Enabled=true;
         
      }
        // Activates the move button if there are checked items.
      private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
      {
         if(e.NewValue==CheckState.Unchecked)
         {
            if(checkedListBox1.CheckedItems.Count==1)
            {
               button2.Enabled = false;
            }
         }
         else
         {
            button2.Enabled = true;
         }
      }

        // Saves the items to a file.
      private void button3_Click(object sender, System.EventArgs e)
      {   
         // Insert code to save a file.
         listBox1.Items.Clear();
         IEnumerator myEnumerator;
         myEnumerator = checkedListBox1.CheckedIndices.GetEnumerator();
         int y;
         while (myEnumerator.MoveNext() != false)
         {
            y =(int) myEnumerator.Current;
            checkedListBox1.SetItemChecked(y, false);
         }
         button3.Enabled = false ;
      }        
    }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Data.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::IO ;

public __gc class Form1 : public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::CheckedListBox* checkedListBox1;
   System::Windows::Forms::TextBox* textBox1;
   System::Windows::Forms::Button* button1;
   System::Windows::Forms::Button* button2;
   System::Windows::Forms::ListBox* listBox1;
   System::Windows::Forms::Button* button3;
   System::ComponentModel::Container* components;

public:
   Form1()
   {
      InitializeComponent();

      // Sets up the initial objects in the CheckedListBox.
      String* myFruit[] = {S"Apples", S"Oranges",S"Tomato"};
      checkedListBox1->Items->AddRange(myFruit);

      // Changes the selection mode from double-click to single click.
      checkedListBox1->CheckOnClick = true;
   }

protected:
   void Dispose( bool disposing )
   {
      if( disposing )
      {
         if (components != 0) 
         {
            components->Dispose();
         }
      }
      Form::Dispose( disposing );
   }

private:
   void InitializeComponent()
   {
      this->components = new System::ComponentModel::Container();
      this->textBox1 = new System::Windows::Forms::TextBox();
      this->checkedListBox1 = new System::Windows::Forms::CheckedListBox();
      this->listBox1 = new System::Windows::Forms::ListBox();
      this->button1 = new System::Windows::Forms::Button();
      this->button2 = new System::Windows::Forms::Button();
      this->button3 = new System::Windows::Forms::Button();
      this->textBox1->Location =  System::Drawing::Point(144, 64);
      this->textBox1->Size =  System::Drawing::Size(128, 20);
      this->textBox1->TabIndex = 1;
      this->textBox1->TextChanged += new System::EventHandler(this, &Form1::textBox1_TextChanged);
      this->checkedListBox1->Location =  System::Drawing::Point(16, 64);
      this->checkedListBox1->Size =  System::Drawing::Size(120, 184);
      this->checkedListBox1->TabIndex = 0;
      this->checkedListBox1->ItemCheck += new System::Windows::Forms::ItemCheckEventHandler(this, &Form1::checkedListBox1_ItemCheck);
      this->listBox1->Location =  System::Drawing::Point(408, 64);
      this->listBox1->Size =  System::Drawing::Size(128, 186);
      this->listBox1->TabIndex = 3;
      this->button1->Enabled = false;
      this->button1->Location =  System::Drawing::Point(144, 104);
      this->button1->Size =  System::Drawing::Size(104, 32);
      this->button1->TabIndex = 2;
      this->button1->Text = S"Add Fruit";
      this->button1->Click += new System::EventHandler(this, &Form1::button1_Click);
      this->button2->Enabled = false;
      this->button2->Location =  System::Drawing::Point(288, 64);
      this->button2->Size =  System::Drawing::Size(104, 32);
      this->button2->TabIndex = 2;
      this->button2->Text = S"Show Order";
      this->button2->Click += new System::EventHandler(this, &Form1::button2_Click);
      this->button3->Enabled = false;
      this->button3->Location =  System::Drawing::Point(288, 104);
      this->button3->Size =  System::Drawing::Size(104, 32);
      this->button3->TabIndex = 2;
      this->button3->Text = S"Save Order";
      this->button3->Click += new System::EventHandler(this, &Form1::button3_Click);
      this->AutoScaleBaseSize =  System::Drawing::Size(5, 13);
      this->ClientSize =  System::Drawing::Size(563, 273);

      System::Windows::Forms::Control* temp0 [] = {this->listBox1,
         this->button3,
         this->button2,
         this->button1,
         this->textBox1,
         this->checkedListBox1};

      this->Controls->AddRange(temp0);
      this->Text = S"Fruit Order";
   }

   // Adds the string if the text box has data in it.

   void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/)
   {
      if(!textBox1->Text->Equals(S""))
      {
         if(checkedListBox1->CheckedItems->Contains(textBox1->Text)== false)
            checkedListBox1->Items->Add(textBox1->Text,CheckState::Checked);
         textBox1->Text = S"";
      }

   }
   // Activates or deactivates the Add button.
   void textBox1_TextChanged(Object* /*sender*/, System::EventArgs* /*e*/)
   {
      if (textBox1->Text->Equals(S""))
      {
         button1->Enabled = false;
      }
      else
      {
         button1->Enabled = true;
      }

   }

   // Moves the checked items from the CheckedListBox to the listBox.
   void button2_Click(Object* /*sender*/, System::EventArgs* /*e*/)
   {
      listBox1->Items->Clear();
      button3->Enabled=false;
      for (int i=0; i< checkedListBox1->CheckedItems->Count;i++)
      {
         listBox1->Items->Add(checkedListBox1->CheckedItems->Item[i]);
      }
      if (listBox1->Items->Count>0)
         button3->Enabled=true;

   }
   // Activates the move button if there are checked items.
   void checkedListBox1_ItemCheck(Object* /*sender*/, ItemCheckEventArgs* e)
   {
      if(e->NewValue==CheckState::Unchecked)
      {
         if(checkedListBox1->CheckedItems->Count==1)
         {
            button2->Enabled = false;
         }
      }
      else
      {
         button2->Enabled = true;
      }
   }

   // Saves the items to a file.
   void button3_Click(Object* /*sender*/, System::EventArgs* /*e*/)
   {   
      // Insert code to save a file.
      listBox1->Items->Clear();
      IEnumerator* myEnumerator;
      myEnumerator = checkedListBox1->CheckedIndices->GetEnumerator();
      int y;
      while (myEnumerator->MoveNext() != false)
      {
         y =*__try_cast<Int32*>( myEnumerator->Current );
         checkedListBox1->SetItemChecked(y, false);
      }
      button3->Enabled = false ;
   }        
};

[STAThread]
int main() 
{
   Application::Run(new Form1());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Windows.Forms

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

アセンブリ: System.Windows.Forms (System.Windows.Forms.dll 内)

参照

CheckedListBox メンバ | System.Windows.Forms 名前空間 | ListBox