Deleting checked items in CheckedListBox

  • Thread starter Thread starter Robke
  • Start date Start date
R

Robke

Hi,

I've had problems deleting checked items from CheckedListBox. Tried searching
the web for solution and then a nice idea came into my mind. Recursion!

So I wrote a simple function:

private void deleteItems(int index)
{
if (checkedListBox1.Items.Count <= index)
return;
else
{
if (checkedListBox1.GetItemCheckState(index) == CheckState.Checked)
{
checkedListBox1.Items.RemoveAt(index);
deleteItems(index);
}
else deleteItems(index+1);
}
return;
}

private void button1_Click(object sender, System.EventArgs e)
{
deleteItems(0);
}

Regards,
Robke
 
Hi Robke,

Isn't it easier if you do something like the following

while(this.checkedListBox1.CheckedIndices.Count > 0)
{
this.checkedListBox1.Items.RemoveAt(checkedListBox1.CheckedIndices[0]);
}
 
Back
Top