Search code examples
c#.neteventscheckedlistbox

.NET CheckedListBox first item first click


I choose an entry from database and then load the list of files to checkedListBox2 and check the ones that relating to my entry in the database. It's all in the form of checkedListBox2s. Checked items are related to database entry, unchecked not.

The problem is that when I click on the first item (index 0) of checkedListBox2, and that item is checked, function checkedListBox2.GetItemChecked() returns false, instead of true, and unchecking my first item (my SelectedIndexChanged event doesn't have code that unchecking items). That happens only if I make my first click on that first item (if I click before on another any item, all is working fine). Also all works fine if the first item is unchecked.

 private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (checkedListBox2.GetItemChecked(checkedListBox2.SelectedIndex) == true)
        {
           ...
           ...
        else
        {
           ....
        }
    }

Can't understand what is wrong. Firstly I thought that the problem is with the event rising during load of checkedListBox2, but same problem even after loading. Am I missing something?


Solution

  • Think about it.
    The first item is the selected item.
    The event is SelectedIndexChanged.
    You can click the first (index 0) 100 times and that event is not going to fired.

    You need to hook into a different event.

    Better yet do it via two way binding.