Question

Hi on this site I found how to delete multiple checkedbox objects in a checklistbox
How to delete multiple checked items from CheckedListBox?

But it's not working for me.
My previous partner who handled this project before I did saved something in Global.answer class. I have tried to modify that script like this:

for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--) {
    if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked) 
    {
        Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
    }
}

It can delete one checkbox correctly, but when I check for two or more checkboxes, it goes wrong... I wonder how to do it correctly.

this is my delete button

private void buttonDelete_Click(object sender, EventArgs e)
    {
        if (checkedListBoxAnswers.SelectedIndices.Count < 1)
        {
            MessageBox.Show(this, "Please select answer to be deleted");
        }
        else
        {
            for (int i = checkedListBoxAnswers.Items.Count - 1; i >= 0; i--)
            {
                if (checkedListBoxAnswers.GetItemCheckState(i) == CheckState.Checked)
                {
                    Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);
                }
            }
            updateCheckListBoxAnswers();
        }
    }
Était-ce utile?

La solution 3

you can try this...

Global.answers.RemoveAt(i);

Autres conseils

in-fact here is correct code:

 CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;

            for (int i = checkedItemColl.Count; i > 0; i--)
            {
                int index = checkedItemColl[i - 1];
                checkedListBoxAnswers.Items.Remove(index);

            }

Because you using this code

Global.answers.RemoveAt(checkedListBoxAnswers.SelectedIndex);

this will delete only the selected first item in the list, you must pass the index of selected Checked Item ,So it will delete that item only, This is how you get all checked Item from List:

CheckedListBox.CheckedItemCollection checkedItemColl = checkedListBoxAnswers.CheckedItems;

        for (int i = checkedItemColl.Count; i > 0; i--)
        {
            int index = checkedItemColl[i - 1];
            checkedListBoxAnswers.Items.Remove(index);

        }

remove comments and test the code. hope you will get benifit from it.

hie bro i have sample ... you can evolve tray this in your sweet home....

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

Your are Removing an Object so Parse that Object into Integer by this way

Convert.ToInt32(Object)

in your case it will be..

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(Convert.ToInt32(checkedListBox1.Items[checkedListBox1.SelectedIndex]));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top