Add or remove checked items from checkedlistbox to listbox

Carl_Honcho picture Carl_Honcho · Sep 26, 2012 · Viewed 26.2k times · Source

I kind of new in C#, my problem is how to add checked items from a checkedlistbox to a listbox, and when I uncheck this item remove it from the listbox also.. Thanks!

Answer

Pavel Shageev picture Pavel Shageev · Sep 26, 2012

If you have checkedListBox1 as checkedListBox and your listBox called listBox1, you should add this ItemCheck Event for your checkedListBox

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
  if (e.NewValue == CheckState.Checked)
    listBox1.Items.Add(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
  if (e.NewValue == CheckState.Unchecked)
    listBox1.Items.Remove(checkedListBox1.Items[checkedListBox1.SelectedIndex]);
}