Here is some complete code that does what you want, but I would have to
admit it's a workaround. Basicly it calls the iterating function on the
mouse up event (by which time the item has been checked)
Tim Wilson his sollution sounds good though.
Greetings,
Olle de Zwart
Code follows
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CheckedListBoxCheckItemEvent
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// checkedListBox1
//
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.Items.AddRange(new object[] {
"Apples",
"Oranges",
"Pears"});
this.checkedListBox1.Location = new System.Drawing.Point(8, 8);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(120, 94);
this.checkedListBox1.TabIndex = 0;
this.checkedListBox1.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.checkedListBox1_MouseUp);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(264, 23);
this.label1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 144);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(256, 23);
this.button1.TabIndex = 2;
this.button1.Text = "Show me the fruit!";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 174);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.checkedListBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.ShowMeTheFruit();
}
//
// This is what it is about. On mouse up the item has been checked so
// call the function that goes over the checkbox items and do something
// with it.
//
private void checkedListBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.ShowMeTheFruit();
}
//
// Function to iterate over the checkbox items
// Display it on the form
//
public void ShowMeTheFruit()
{
string fruits = "You've selected";
System.Windows.Forms.CheckedListBox.CheckedItemCollection c =
this.checkedListBox1.CheckedItems;
for(int i = 0; i < c.Count; i++)
{
fruits = fruits + " "+ c.ToString();
}
this.label1.Text = fruits;
}
}
}