CheckedListBox and ItemCheck Event

L

Lanny Thompson

Is there an event similar to ItemCheck, within the CheckedListBox
class, that is called after a list item is checked and updated with
the new check value?

I currently have a method that iterates through the CheckedItems
collection of a CheckListBox control. My problem is the CheckedItems
collection doesn't have the correct count because of the ItemCheck
event occurring before the checked state is changed.

Thanks,
Lan
 
T

Tim Wilson

Could you use the ItemCheck event and the ItemCheckEventArgs, that is passed
in as a parameter, to check the state of the changed item? The
ItemCheckEventArgs instance that is passed in has the following properties:
"Index" of the checked/unchecked item
"CurrentValue" (checked state) of the checked/unchecked item
"NewValue" (checked state) of the checked/unchecked item
You might be able to run through the CheckItems collection and then handle
this changed item as a special case afterwards.

Does this work in your situation?
 
O

O.S. de Zwart

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;

}

}

}
 
L

Lanny Thompson

That will work, but I decided to just use the SelectedIndexChanged
event. It appears to be called whenever an item is selected (even if
it is the same one). It is also called after an ItemCheck event, so
the CheckItems collection is correct at that time.

Thanks for the help.
Lanny
 
T

Tim Wilson

There's just a bit of overhead with doing it that way since, as you pointed
out, the SelectedIndexChanged event gets called not only when an item is
checked/unchecked but when the highlighted item changes as well. So the code
that you use will potentially be executed at times when it's not
specifically necessary. But this will depend on your program and what you're
actually doing. If this makes sense in the context of your application then
it's purely a developer's decision.

--
Tim Wilson
..Net Compact Framework MVP

Lanny Thompson said:
That will work, but I decided to just use the SelectedIndexChanged
event. It appears to be called whenever an item is selected (even if
it is the same one). It is also called after an ItemCheck event, so
the CheckItems collection is correct at that time.

Thanks for the help.
Lanny

"Tim Wilson" <Tim_Wilson@[DieSpamDie]Rogers.com> wrote in message
Could you use the ItemCheck event and the ItemCheckEventArgs, that is passed
in as a parameter, to check the state of the changed item? The
ItemCheckEventArgs instance that is passed in has the following properties:
"Index" of the checked/unchecked item
"CurrentValue" (checked state) of the checked/unchecked item
"NewValue" (checked state) of the checked/unchecked item
You might be able to run through the CheckItems collection and then handle
this changed item as a special case afterwards.

Does this work in your situation?
 
L

Lanny Thompson

Actually the CheckedListBox CheckOnClick property is true, so the control
has to update every time the user clicks an item because the check state of
an item is switched.

So this method works, but I agree that this isn't an optimal solution for
any other CheckedListBox without CheckOnClick set to true.

Lan
 
T

Tim Wilson

Keep in mind that the user can change the index using the keyboard so the
user doesn't have to change the check state of an item every time the
selected index is changed. So even with the CheckOnClick property set to
true there will still be a potential amount of extra processing being done
depending on how the end user chooses to cursor through and select items in
the list. But I'm not trying to argue here, I just wanted to bring this
point up so that you're aware of this possible overhead.
 
O

O.S. de Zwart

Good call my mouse up sollution would indeed fail with the user and his or
her keyboard.

Olle de Zwart
 
L

Lanny Thompson

Ahh. I forgot about that. You're right. Fortunately I can live with the
overhead in this instance, but definitely not in others. I guess I could
also add a check to return without updating if the CheckItems count doesn't
change.

Thanks for pointing that out. I appreciate it and definitely don't view it
as arguing.

Lan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top