Aung Kaung Hein
Aung Kaung Hein

Reputation: 1566

Bind ComboBox Dynamically with CheckedListBox Items?

I want to bind a ComboBox with checked items from CheckedListBox based on selection made by user.

This is how I bind ComboBox:

    private void LoadFOCOutlets()
    {
        ArrayList outletList = new ArrayList();
        Outlet objOutlet = new Outlet();

        for (int i = 0; i < customCheckListBoxOutletList.CheckedItems.Count; i++)
        { 
            objOutlet = (Outlet)customCheckListBoxOutletList.Items[i];
            outletList.Add(objOutlet);
        }            

        objOutlet.OutletID = 0;
        objOutlet.OutletName = "Select Outlet";
        outletList.Insert(0, objOutlet);

        cmbFOCOutlets.DataSource = outletList;
        cmbFOCOutlets.DisplayMember = "OutletName";
        cmbFOCOutlets.ValueMember = "OutletID";
        cmbFOCOutlets.DropDownStyle = ComboBoxStyle.DropDownList;
    }

So, every time when a user check a new item, it should re-bind the ComboBox. The above code works fine.

But which event of CheckedListBox can I use to re-bind the ComboBox after a new item has been checked? I tried using ItemCheck Event. But It doesn't count the current selection.

Any help will be very much appreciated.

Upvotes: 0

Views: 1464

Answers (2)

RandallM77
RandallM77

Reputation: 1

The following is excerpted from Which CheckedListBox event triggers after a item is checked? Which CheckedListBox event triggers after a item is checked? (answer #3)

softburger states: I tried this and it worked: (seems to work for me, too)

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();        
}

The idea is, as mentioned in many posts, CheckedListBox has an ItemCheck event, but no ItemChecked event.

To get around this, the ItemCheck handler assignment is briefly suspended(within the ItemCheck handler routine itself (!?)),

during which time the CheckedListBox's SetItemCheckState method is invoked for the newly checked item, (which should place the item in the CheckedListBox's CheckedItems collection)

and then the ItemCheck handler is then reassigned.

i.e.

// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;

and now you can (finally) get all theCheckedListBox Checked Items from its CheckedItems collection. (great hack, if you ask me)

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

Try this event

private void CheckedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   //Your code here
}

(Or)

private void CheckedListBox1_ItemCheck(object sender, EventArgs e)
{
   //Your code here
}

Refer This

Upvotes: 1

Related Questions