-1

I have an exception here Application.Run(new Form1()) when I check not first item in CheckedListBox. Here's my code example

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private List<bool> l = new List<bool>() { true, false, true, true, true};

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            checkedListBox1.Items.Add(i);
        }
    }

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        for (int i = 0; i < 5; i++)
        {
            if (l[i] == true)
            {
                checkedListBox1.Items.Remove(i);
            }
        }
    }
}

Is there anybody who knows how to fix it? Thanks in advance.

Denis Lolik
  • 299
  • 1
  • 4
  • 11
  • Hint: you iterate through 5 elements of list `l`, but removing some items from `checkedListBox1`, so there will not be 5 elements there after such removal... – Konrad Kokosa Nov 02 '14 at 17:03
  • `for (int i = 0; i < checkedListBox1.Items.Count; i++)` – Mikko Viitala Nov 02 '14 at 17:09
  • But why does the step-through debugging show me that everything is ok? Could you check this on your computer please? – Denis Lolik Nov 02 '14 at 17:18
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Preston Guillot Nov 02 '14 at 18:20

1 Answers1

0

The issue is that you're checking an item, firing the event associated with it (checkedListBox1_ItemCheck), and while the event is executing you are removing it from checkedListBox1. Therefore the event is no longer attached to an item, and the form throws an exception.

You can confirm this by toggling the first check box which should behave fine.

You need to rethink how you would like this to work. I don't know why you are designing it this way, but there is likely a better method. Why are you deleting items from checkedListBox1 on the check toggle? You can use an independent button to accomplish this.

grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • Thanks, but it doesn't work. Just check it. And exception is here Application.Run(new Form1()). Why? – Denis Lolik Nov 02 '14 at 17:25
  • @AlexSacket: I updated my answer. Please let me know if you need it clarified further. – grovesNL Nov 02 '14 at 17:40
  • I want to delete the items which are incompatible with the checked items. Incompatible items are marked as true in the list `l`. When I toggle the second item which is false, cause it cannot be incompatible with itself, all "true" items have to be removed. Firstly I wanted to make it disabled, but it's impossible in that control. Therefore I decided to remove it. If you do not figure this out I can send you a picture that shows how it must work. – Denis Lolik Nov 02 '14 at 17:56
  • @AlexSacket: don't delete items when the checkboxes are checked. Delete them when some other action occurs (such as, say, clicking a button that says "remove checked items"). – siride Nov 02 '14 at 18:06
  • @siride: the checked item is not deleted, because this one is marked as false in the `l` list, therefore it's skipped – Denis Lolik Nov 02 '14 at 18:14
  • @AlexSacket: I think you may want to filter the list in some way instead (outside of the checked list box). This doesn't seem like the best approach because the check behavior isn't intuitive. Consider splitting your check box into multiple boxes for those conditions. You can add a diagram to your original post if you want to clarify the behavior further. – grovesNL Nov 02 '14 at 18:24