Hi @John Pike , Welcome to Microsoft Q&A.
Just use the correct logic in the checklistbox: add when checked, remove when unchecked.
button1, button2 are remove and removeAll respectively.
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var selectItem = checkedListBox1.SelectedItem;
bool isChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);
if (!isChecked)
{
listBox1.Items.Add(selectItem);
}
else
{
listBox1.Items.Remove(selectItem);
}
//Get the correct amount at now
BeginInvoke(new Action(() =>
{
textBox1.Text = checkedListBox1.CheckedItems.Count.ToString();
}));
}
private void button1_Click(object sender, System.EventArgs e)
{
if (listBox1.SelectedItem == null)
return;
var item = listBox1.SelectedItem;
int index = checkedListBox1.Items.IndexOf(item);
checkedListBox1.SelectedItem = item;
checkedListBox1.SetItemChecked(index, false);
textBox1.Text = checkedListBox1.CheckedItems.Count.ToString();
}
private void button2_Click(object sender, System.EventArgs e)
{
for (int i = checkedListBox1.Items.Count - 1; i >= 0; i--)
{
checkedListBox1.SetItemChecked(i, false);
}
listBox1.Items.Clear();
textBox1.Text = checkedListBox1.CheckedItems.Count.ToString();
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.