Ahmadali Shafiee
Ahmadali Shafiee

Reputation: 4657

Checked List Box's Checked Items doesn't work

I have this Code for draw a diagram in a PictureBox:

    private void ChkLiboData_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        Refresh();
        try
        {
            foreach (DataClass d in ChkLiboData.CheckedItems)
            {
                if (d.r == null && d.g == null && d.b == null)
                {
                    Random r = new Random();

                    d.r = r.Next(0, 255);
                    d.g = r.Next(0, 255);
                    d.b = r.Next(0, 255);

                    DrawDiagram(d.DataList, (int)d.r, (int)d.g, (int)d.b);
                }
                else
                {
                    DrawDiagram(d.DataList, (int)d.r, (int)d.g, (int)d.b);
                }
                Refresh();
            }
        }
        catch { }
    }

but in debug mod when I check an Item and I looked at ChkLiboData.CheckedItems I couldn't see any Items in ChkLiboData.CheckedItems. what am I have to do???

Upvotes: 1

Views: 712

Answers (1)

Fischermaen
Fischermaen

Reputation: 12458

The event ItemCheck is raised when the checked status of an item is about to be changed. It isn't changed already. Let me show that with an example. The CheckedListBox contains 3 items "A", "B" and "C". No item is checked. Now the user checks the item "A". The event ItemCheck is fired. The property CheckedItems contains no item. In the event args e (of type ItemCheckEventArgs) you can find the index of the item which checked state is changing, a property CurrentValue containing the checked state before and a property NewValue containing the new checked state. If the user then checks item "B", the event is fired again. This time the property CheckedItems contains one item "A". a.s.o.

BTW: You can set the property NewValue in the ItemCheck event. This gives you the possibility e.g. to prevent an item to be checked.

Upvotes: 4

Related Questions