CheckedListBox: get selection state : CheckedListBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » CheckedListBox 
23.28.1.CheckedListBox: get selection state
CheckedListBox: get selection state
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class CheckedListBoxes : Form
{
  CheckedListBox clb;
  Button btnToggle;
  Button btnClear;
  String str;

  public CheckedListBoxes()
  {
    Size = new Size(300,400);
    this.Load += new EventHandler(this_Load);

    clb = new CheckedListBox();
    clb.Parent = this;
    clb.Location = new Point(10,10);
    clb.Size = new Size(ClientSize.Width - 20, Height - 240);
    clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    clb.BorderStyle = BorderStyle.Fixed3D;
    clb.MultiColumn = true;
    clb.ScrollAlwaysVisible = true;
    clb.ThreeDCheckBoxes = true;  
    clb.CheckOnClick = true;
    clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);

    //  Toggle Indeterminate Button
    btnToggle = new Button();
    btnToggle.Parent = this;
    btnToggle.Text = "Toggle Indeterminate";
    btnToggle.Size = new Size((int)(Font.Height * .75* btnToggle.Text.Length, Font.Height + 10);
    btnToggle.Location = new Pointclb.Left, clb.Bottom + 10);
    btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    btnToggle.Click += new System.EventHandler(btnToggle_Click);

    //  Clear Button
    btnClear = new Button();
    btnClear.Parent = this;
    btnClear.Text = "Clear All";
    btnClear.Size = new Size((int)(Font.Height * .75* btnClear.Text.Length, Font.Height + 10);
    btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
    btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
    btnClear.Click += new System.EventHandler(btnClear_Click);

    clb.Items.Add("12345");
      clb.Items.Add("67890");      
      clb.Items.Add("7890");      
      clb.Items.Add("890");            
  }  

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

  private void btnToggle_Click(object sender, EventArgs e)
  {
    for (int i = 0; i <= (clb.Items.Count - 1); i++)
    {
      if (clb.GetItemCheckState(i== CheckState.Checked)
      {
        clb.SetItemCheckState(i, CheckState.Indeterminate);  
      }
      else if (clb.GetItemCheckState(i== CheckState.Indeterminate)
      {
        clb.SetItemCheckState(i, CheckState.Checked);  
      }
    }  
  }

  private void btnClear_Click(object sender, EventArgs e)
  {
    clb.ClearSelected();

    for (int i = 0; i <= (clb.Items.Count - 1); i++)
    {
      clb.SetItemChecked(i, false);  
    }
  }

  private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
  {
    Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
    Console.WriteLine("Current Index:" + e.Index.ToString() );
    Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
    Console.WriteLine("New Value:" + e.NewValue.ToString() );
  }

  private void clb_SelectedIndexChanged(object sender, EventArgs e)
  {
    Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
    Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
  }

  private void this_Load(object sender, EventArgs e)
  {
    clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
  }
}
23.28.CheckedListBox
23.28.1.CheckedListBox: get selection stateCheckedListBox: get selection state
23.28.2.CheckedListBox: set item selectedCheckedListBox: set item selected
23.28.3.CheckedListBox: checked and selected item countCheckedListBox: checked and selected item count
23.28.4.CheckedListBox: selected change eventCheckedListBox: selected change event
23.28.5.Copy selected CheckedItems to another CheckedListBox
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.