samedi 13 mars 2021

Uncheck all CheckBoxes using Linq

I have three checkboxes and after selection an entry can be made in the listbox (see below). How can I remove the entry from the listbox again when i want to change the state of the checkbox (unchecked)? Many thanks in advance for all your help!

private CheckBox[] checkboxes;
private string[] cbx;

checkboxes = new[] {checkBox1, checkBox2, checkBox3};

checkBox1.Tag =  "string1"; 
checkBox2.Tag =  "string2";
checkBox3.Tag =  "string3"; 

foreach (var checky in checkboxes)
{
   checky.CheckedChanged += CheckBox_CheckedChanged;
}

private void CheckBox_CheckedChanged(object sender, EventArgs e)
{
   cbx = checkboxes.Where(checky => checky.Checked)
  .Select(checky => checky.Tag)
  .Cast<string>()
  .ToArray();

  listBox4.Items.AddRange(cbx);

 }








Aucun commentaire:

Enregistrer un commentaire