Which CheckedListBox event triggers after a item i

2019-01-06 19:19发布

I have a CheckedListBox where I want an event after an item is checked so that I can use CheckedItems with the new state.

Since ItemChecked is fired before CheckedItems is updated it won't work out of the box.

What kind of method or event can I use to be notified when the CheckedItems is updated?

11条回答
forever°为你锁心
2楼-- · 2019-01-06 20:04

Don't know if this applies but I wanted to use a checklistbox to filter results. So as the user checked and unchecked items I wanted the list to show\hide items.

I was having some issues which led me to this post. Just wanted to share how I did it without anything special.

Note: I have CheckOnClick = true but it would probably still work without

The event I use is "SelectedIndexChanged"

the enumeration I use is ".CheckedItems"

This give the results I think we may 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
        }
   }

}
查看更多
Viruses.
3楼-- · 2019-01-06 20:04

Assuming you want to preserve the arguments from ItemCheck but get notified after the model was changed it should look like that:

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();
}
查看更多
Viruses.
4楼-- · 2019-01-06 20:06

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();        
}
查看更多
做自己的国王
5楼-- · 2019-01-06 20:12

Although not ideal, you can calculate the CheckedItems using the arguments that are passed through to the ItemCheck event. If you look at this example on MSDN, you can work out whether the newly changed item has been checked or unchecked, which leaves you in a suitable position to work with the items.

You could even create a new event that fires after an item is checked, which would give you exactly what you wanted if you wished.

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-06 20:12

This works, not sure how elegant it is though!

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
查看更多
登录 后发表回答