0

I have a checkedListBox which puts the string to a textbox when an item is checked.

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
     if (checkedListBox1.GetItemCheckState(e.Index) == CheckState.Checked)
     {
          textBox1.Text = textBox1.Text + checkedListBox1.Items[e.Index].ToString();
     }
} 

This doesn't seem to work properly, When I check an item it doesn't do anything and when I unchecked an item the string gets added to the textbox.

How do I check if the item is going to be checked or unchecked, the code I have seems to be working if the checkbox WAS checked.

sabre
  • 207
  • 6
  • 18

1 Answers1

2

The state of the item hasn't been "committed" yet. Use e.NewValue instead:

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Checked)
        {
            textBox1.Text = textBox1.Text + checkedListBox1.Items[e.Index].ToString();
        }
    } 
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40