What event CheckedListBox is triggered after checking an element?

I have a CheckedListBox where I want an event after checking an item so that I can use CheckedItems in a new state.

Since ItemChecked starts before CheckedItems is updated, it will not work out of the box.

What method or event can I use to notify me when updating CheckedItems?

+76
c # winforms checkedlistbox
Sep 08 '10 at 10:18
source share
12 answers

You can use the ItemCheck event if you also check the new state of the ItemCheck item. It is available in event arguments, like e.NewValue . If the NewValue checkbox is NewValue current element together with the collection itself in its own logic:

  private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { List<string> checkedItems = new List<string>(); foreach (var item in checkedListBox1.CheckedItems) checkedItems.Add(item.ToString()); if (e.NewValue == CheckState.Checked) checkedItems.Add(checkedListBox1.Items[e.Index].ToString()); else checkedItems.Remove(checkedListBox1.Items[e.Index].ToString()); foreach (string item in checkedItems) { ... } } 

As another example, to determine if a collection will be empty after this element is (un-) checked:

 private void ListProjects_ItemCheck(object sender, ItemCheckEventArgs args) { if (ListProjects.CheckedItems.Count == 1 && args.NewValue == CheckState.Unchecked) // The collection is about to be emptied: there just one item checked, and it being unchecked at this moment ... else // The collection will not be empty once this click is handled ... } 
+67
Sep 08 '10 at 11:00
source share

There are many related StackOverflow posts on this ... Like Branimir's solution , here are two more simple ones:

Delayed execution in ItemCheck (also here ):

  void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) { this.BeginInvoke((MethodInvoker) ( () => Console.WriteLine(checkedListBox1.SelectedItems.Count))); } 

Using the MouseUp event :

  void checkedListBox1_MouseUp(object sender, MouseEventArgs e) { Console.WriteLine(checkedListBox1.SelectedItems.Count); } 

I prefer the first option, as the second will lead to false positives (i.e., too often triggered).

+27
Jun 05 '13 at 9:43 on
source share

I tried this and it worked:

 private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e) { CheckedListBox clb = (CheckedListBox)sender; // Switch off event handler clb.ItemCheck -= clbOrg_ItemCheck; clb.SetItemCheckState(e.Index, e.NewValue); // Switch on event handler clb.ItemCheck += clbOrg_ItemCheck; // Now you can go further CallExternalRoutine(); } 
+20
Jul 07 '13 at 11:26
source share

Derive from CheckedListBox and implement

 /// <summary> /// Raises the <see cref="E:System.Windows.Forms.CheckedListBox.ItemCheck"/> event. /// </summary> /// <param name="ice">An <see cref="T:System.Windows.Forms.ItemCheckEventArgs"/> that contains the event data. /// </param> protected override void OnItemCheck(ItemCheckEventArgs e) { base.OnItemCheck(e); EventHandler handler = AfterItemCheck; if (handler != null) { Delegate[] invocationList = AfterItemCheck.GetInvocationList(); foreach (var receiver in invocationList) { AfterItemCheck -= (EventHandler) receiver; } SetItemCheckState(e.Index, e.NewValue); foreach (var receiver in invocationList) { AfterItemCheck += (EventHandler) receiver; } } OnAfterItemCheck(EventArgs.Empty); } public event EventHandler AfterItemCheck; public void OnAfterItemCheck(EventArgs e) { EventHandler handler = AfterItemCheck; if (handler != null) handler(this, e); } 
+9
Aug 28 '13 at 12:11
source share

Although not ideal, you can compute CheckedItems using the arguments passed to the ItemCheck event. If you look at this example on MSDN , you can decide whether a new or changed item has been checked or unchecked, which leaves you in a suitable position for working with items.

You can even create a new event that fires after checking the element, which will give you exactly what you wanted if you want.

+4
Sep 08 '10 at 11:01
source share

After some tests, I saw that the SelectedIndexChanged event is fired after the ItemCheck event. Store CheckOnClick True property

Best coding

+4
Aug 11 '14 at 19:02
source share

It works, but not sure how elegant it is!

 Private Sub chkFilters_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFilters.ItemCheck Static Updating As Boolean If Updating Then Exit Sub Updating = True Dim cmbBox As CheckedListBox = sender Dim Item As ItemCheckEventArgs = e If Item.NewValue = CheckState.Checked Then cmbBox.SetItemChecked(Item.Index, True) Else cmbBox.SetItemChecked(Item.Index, False) End If 'Do something with the updated checked box Call LoadListData(Me, False) Updating = False End Sub 
+2
Jun 10 '13 at 19:35
source share

I don't know if this is applicable, but I wanted to use a checklist to filter the results. Since the user checked and unchecked the elements, I wanted the list to show / hide the elements.

I had some problems that led me to this post. I just wanted to share how I did it without much attention.

Note. I have CheckOnClick = true , but it will probably work without

The event I'm using is " SelectedIndexChanged "

the listing I'm using is " .CheckedItems "

This gives the results that I think we can expect. So simplified it comes down to ....

 private void clb1_SelectedIndexChanged(object sender, EventArgs e) { // This just spits out what is selected for testing foreach (string strChoice in clb1.CheckedItems) { listBox1.Items.Add(strChoice); } //Something more like what I'm actually doing foreach (object myRecord in myRecords) { if (clb1.CheckItems.Contains(myRecord["fieldname"]) { //Display this record } } } 
+1
Aug 14 '15 at 17:26
source share

Assuming you want to save the arguments from ItemCheck but get notified after changing the model, it should look like this:

 CheckedListBox ctrl = new CheckedListBox(); ctrl.ItemCheck += (s, e) => BeginInvoke((MethodInvoker)(() => CheckedItemsChanged(s, e))); 

Where CheckedItemsChanged could be:

 private void CheckedItemsChanged(object sender, EventArgs e) { DoYourThing(); } 
+1
Feb 06 '18 at 14:40
source share

I am using a timer to solve this problem. Start the timer using the ItemCheck event. Take action at the Timer Tick event.

This works whether the item is checked with a mouse click or by pressing the space bar. We will take advantage of the fact that an item that has just been verified (or not verified) is always the selected item.

The timer interval can be as low as 1. By the time the Tick event occurs, a new status of "Checked" will be set.

This VB.NET code shows the concept. There are many options you can use. You can increase the timer interval so that the user can change the status of the check by several elements before taking action. Then, in the Tick event, make a sequential pass of all the elements in the list or use its collection of CheckedItems to take appropriate measures.

This is why we first turned off the Timer in the ItemCheck event. Disable, then Enable, causes the interval period to restart.

 Private Sub ckl_ItemCheck(ByVal sender As Object, _ ByVal e As System.Windows.Forms.ItemCheckEventArgs) _ Handles ckl.ItemCheck tmr.Enabled = False tmr.Enabled = True End Sub Private Sub tmr_Tick(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles tmr.Tick tmr.Enabled = False Debug.Write(ckl.SelectedIndex) Debug.Write(": ") Debug.WriteLine(ckl.GetItemChecked(ckl.SelectedIndex).ToString) End Sub 
0
Apr 10 '15 at 20:29
source share

In normal behavior, when we test a single element, the state of the element's check will change before the event handler is called. But CheckListBox has a different behavior: the event handler is called before the element's check state changes, which makes it difficult to fix our tasks.

In my opinion, in order to solve this problem, we must postpone the event handler.

 private void _clb_ItemCheck(object sender, ItemCheckEventArgs e) { // Defer event handler execution Task.Factory.StartNew(() => { Thread.Sleep(1000); // Do your job at here }) .ContinueWith(t => { // Then update GUI at here },TaskScheduler.FromCurrentSynchronizationContext());} 
0
Nov 17 '15 at 5:32
source share

Set the checkCheckbox property CheckOnClick to true and try using the MouseUp event.

0
Sep 14 '16 at 9:01
source share



All Articles