Export (0) Print
Expand All
Expand Minimize
1 out of 2 rated this helpful - Rate this topic

CheckState Enumeration

Specifies the state of a control, such as a check box, that can be checked, unchecked, or set to an indeterminate state.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public enum class CheckState
public enum CheckState
public enum CheckState
 Member nameDescription
Supported by the .NET Compact FrameworkCheckedThe control is checked. 
Supported by the .NET Compact FrameworkIndeterminateThe control is indeterminate. An indeterminate control generally has a shaded appearance. 
Supported by the .NET Compact FrameworkUncheckedThe control is unchecked. 

Multiple methods in CheckBox, CheckedListBox, and ItemCheckEventArgs use this enumeration.

NoteNote

Use an indeterminately checked control when you do not want to set a default value.

The following code example demonstrates the use of the CheckState enumeration.

#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 ref 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.
      array<String^>^myFruit = {"Apples","Oranges","Tomato"};
      checkedListBox1->Items->AddRange( myFruit );
      
      // Changes the selection mode from double-click to single click.
      checkedListBox1->CheckOnClick = true;
   }

public:
   ~Form1()
   {
      if ( components != nullptr )
      {
         delete components;
      }
   }

private:
   void InitializeComponent()
   {
      this->components = gcnew System::ComponentModel::Container;
      this->textBox1 = gcnew System::Windows::Forms::TextBox;
      this->checkedListBox1 = gcnew System::Windows::Forms::CheckedListBox;
      this->listBox1 = gcnew System::Windows::Forms::ListBox;
      this->button1 = gcnew System::Windows::Forms::Button;
      this->button2 = gcnew System::Windows::Forms::Button;
      this->button3 = gcnew 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 += gcnew 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 += gcnew 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 = "Add Fruit";
      this->button1->Click += gcnew 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 = "Show Order";
      this->button2->Click += gcnew 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 = "Save Order";
      this->button3->Click += gcnew System::EventHandler( this, &Form1::button3_Click );
      this->ClientSize = System::Drawing::Size( 563, 273 );
      array<System::Windows::Forms::Control^>^temp0 = {this->listBox1,this->button3,this->button2,this->button1,this->textBox1,this->checkedListBox1};
      this->Controls->AddRange( temp0 );
      this->Text = "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( "" ) )
      {
         if ( checkedListBox1->CheckedItems->Contains( textBox1->Text ) == false )
                  checkedListBox1->Items->Add( textBox1->Text, CheckState::Checked );
         textBox1->Text = "";
      }
   }

   // Activates or deactivates the Add button.
   void textBox1_TextChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      if ( textBox1->Text->Equals( "" ) )
      {
         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[ 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 = safe_cast<Int32>(myEnumerator->Current);
         checkedListBox1->SetItemChecked( y, false );
      }

      button3->Enabled = false;
   }
};

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

import System.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
import System.IO.*;
  
public class Form1 extends 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.get_Items().AddRange(myFruit);
        // Changes the selection mode from double-click to single click.
        checkedListBox1.set_CheckOnClick(true);
    } //Form1

    protected void Dispose(boolean disposing)
    {
        if (disposing) {
            if (components != null) {
                components.Dispose();
            }
        }
        super.Dispose(disposing);
    } //Dispose

    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.set_Location(new System.Drawing.Point(144, 64));
        this.textBox1.set_Size(new System.Drawing.Size(128, 20));
        this.textBox1.set_TabIndex(1);
        this.textBox1.add_TextChanged(new System.EventHandler(
            this.textBox1_TextChanged));
        this.checkedListBox1.set_Location(new System.Drawing.Point(16, 64));
        this.checkedListBox1.set_Size(new System.Drawing.Size(120, 184));
        this.checkedListBox1.set_TabIndex(0);
        this.checkedListBox1.add_ItemCheck(
            new System.Windows.Forms.ItemCheckEventHandler(
            this.checkedListBox1_ItemCheck));
        this.listBox1.set_Location(new System.Drawing.Point(408, 64));
        this.listBox1.set_Size(new System.Drawing.Size(128, 186));
        this.listBox1.set_TabIndex(3);
        this.button1.set_Enabled(false);
        this.button1.set_Location(new System.Drawing.Point(144, 104));
        this.button1.set_Size(new System.Drawing.Size(104, 32));
        this.button1.set_TabIndex(2);
        this.button1.set_Text("Add Fruit");
        this.button1.add_Click(new System.EventHandler(this.button1_Click));
        this.button2.set_Enabled(false);
        this.button2.set_Location(new System.Drawing.Point(288, 64));
        this.button2.set_Size(new System.Drawing.Size(104, 32));
        this.button2.set_TabIndex(2);
        this.button2.set_Text("Show Order");
        this.button2.add_Click(new System.EventHandler(this.button2_Click));
        this.button3.set_Enabled(false);
        this.button3.set_Location(new System.Drawing.Point(288, 104));
        this.button3.set_Size(new System.Drawing.Size(104, 32));
        this.button3.set_TabIndex(2);
        this.button3.set_Text("Save Order");
        this.button3.add_Click(new System.EventHandler(this.button3_Click));
        this.set_ClientSize(new System.Drawing.Size(563, 273));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { 
            this.listBox1, this.button3, this.button2, this.button1, 
            this.textBox1, this.checkedListBox1});
        this.set_Text("Fruit Order");
    } //InitializeComponent

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main

    // Adds the string if the text box has data in it.
    private void button1_Click(Object sender, System.EventArgs e)
    {
        if (!textBox1.get_Text().Equals("")) {
            if (checkedListBox1.get_CheckedItems().Contains(textBox1.get_Text())
                == false) {
                checkedListBox1.get_Items().Add(textBox1.get_Text(), 
                    CheckState.Checked);
            }
            textBox1.set_Text("");
        }
    } //button1_Click

    // Activates or deactivates the Add button.
    private void textBox1_TextChanged(Object sender, System.EventArgs e)
    {
        if (textBox1.get_Text().Equals("")) {
            button1.set_Enabled(false);
        }
        else {
            button1.set_Enabled(true);
        }
    } //textBox1_TextChanged

    // Moves the checked items from the CheckedListBox to the listBox.
    private void button2_Click(Object sender, System.EventArgs e)
    {
        listBox1.get_Items().Clear();
        button3.set_Enabled(false);
        for (int i = 0; i < checkedListBox1.get_CheckedItems().get_Count(); 
            i++) {
            listBox1.get_Items().Add(
                checkedListBox1.get_CheckedItems().get_Item(i));
        }
        if (listBox1.get_Items().get_Count() > 0) {
            button3.set_Enabled(true);
        }
    } //button2_Click

    // Activates the move button if there are checked items.
    private void checkedListBox1_ItemCheck(Object sender, ItemCheckEventArgs e)
    {
        if (e.get_NewValue().Equals(CheckState.Unchecked)) {
            if (checkedListBox1.get_CheckedItems().get_Count() == 1) {
                button2.set_Enabled(false);
            }
        }
        else {
            button2.set_Enabled(true);
        }
    } //checkedListBox1_ItemCheck

    // Saves the items to a file.
    private void button3_Click(Object sender, System.EventArgs e)
    {
        // Insert code to save a file.
        listBox1.get_Items().Clear();
        IEnumerator myEnumerator;
        myEnumerator = checkedListBox1.get_CheckedIndices().GetEnumerator();
        int y;
        while ((myEnumerator.MoveNext() != false)) {
            y = (int)(System.Convert.ToInt32(myEnumerator.get_Current()));
            checkedListBox1.SetItemChecked(y, false);
        }
        button3.set_Enabled(false);
    } //button3_Click
} //Form1

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.