How to add marked items to a checklistbox and how to delete them

1

The objective is that in a cheboxlistbox the elements of a textbox are added, these are marked and then I can select and delete these elements of the checboxlistbox. In the checklistbox I must add items to the list, and when they are added, they appear marked and in addition I can select items from the list and delete the selected item. in the image would be the elements in the lower left. This is the code to add items to the checklistbox

private void btañadir_Click(object sender, EventArgs e)
    {
        if (txtañadir.Text == "")
        {
            MessageBox.Show("Ha de rellenar el formulario.");

        }
        else
        {
            clbidiomas.Items.Add(txtañadir);

            MessageBox.Show("Los datos se han añadido correctamente.");
        }
    }

and this is the delete code (in which I have not implemented anything)

private void btborrar_Click(object sender, EventArgs e)
    {

    }

    
asked by Trackless 28.02.2018 в 18:27
source

1 answer

2

The add method, which you use correctly, returns the index of the item you just added.

Therefore, with some small changes, you can mark the item that you just added:

int index = clbidiomas.Items.Add(txtañadir);
clbidiomas.SetItemChecked(index, true);

The SetItemChecked method, sets the item located in index, with the value that you say (in this case true, or marked)

To remove an item from the list, use .Items.RemoveAt if you know the index to remove.

If not, for example if you would like to remove all that are marked, you must go through the internal collection that says which ones are marked and remove them.

while (checkedListBox1.CheckedItems.Count > 0)
{
     checkedListBox1.Items.Remove(checkedListBox1.CheckedItems[0]);
}
    
answered by 28.02.2018 / 19:02
source