Hi.....This is as part of my project.I am using Visual C#.I have a checkedlistbox and a listbox.Whenever I select an item in the checkedlistbox,it gets added into the listbox.Whenever I deselect that item,it gets removed from the listbox.The problem that I am having is if I double click an item in the checkedlistbox,the checked state of that item remains the same,but its contents are still added into the listbox.For example,if an item in the checkedlistbox has not been selected yet,then on doubleclicking that item,its state is unchecked again.Its contents should get added and then removed from the listbox.However the contents are still only added to the listbox and not removed.How do I handle this?I should check if the doubleclick event is occuring in the selectedindexchange event,but I dont know how to do it.Please help me.

Add a DoubleClick event for the checkedListBox and call the "checkedListBox1_SelectedIndexChanged" event from there. Like this:

Private Sub CheckedListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.DoubleClick
        CheckedListBox1_SelectedIndexChanged(Nothing, Nothing)
    End Sub}

Thanks

Whilst farooqaaa's code might get it done, its cleaner to use the ItemCheck event instead. This event is called every time the check state is changed so if you double click it will fir ethe event twice (once for checking and once for unchecking).

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            //if item checked, add it to listbox
            if (e.NewValue == CheckState.Checked)
            {
                listBox1.Items.Add(checkedListBox1.Items[e.Index]);
            }
            //if item unchecked, remove it
            else
            {
                listBox1.Items.Remove(checkedListBox1.Items[e.Index]);
            }
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //set the currently selected items checked state to the opposite of its current state
            checkedListBox1.SetItemChecked(checkedListBox1.SelectedIndex, !(checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex)));
        }

I've also added some code to the checkedListBox's selectedindex changed event. You dont need to use this, i just prefer it. The code will alter the checked state whenever you select a row. I prefer it this way to the default method of clicking a row to select it and then clicking again to alter its checked state.

Hi....Thanks for the help...I tried what u said...
But it doesnot work at all.
Nothing is getting added into the listbox on selecting an item in the checkedlistbox.
Please help me.

Sorry, I posted the VB version in my last post. Try this:

private void checkedListBox1_DoubleClick(object sender, EventArgs e)
        {
            checkedListBox1_SelectedIndexChanged(null, null);
        }

When you added my code, did you remember to create the relevant Event Handlers and change the control names to match your controls?
I tested this and it worked perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.