Deleting checked items in CheckedListBox

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top