C# 选中列表框';s检查项目不';行不通

C# 选中列表框';s检查项目不';行不通,c#,.net,winforms,c#-4.0,C#,.net,Winforms,C# 4.0,我有在PictureBox中绘制图表的代码: private void ChkLiboData_ItemCheck(object sender, ItemCheckEventArgs e) { Refresh(); try { foreach (DataClass d in ChkLiboData.CheckedItems) { if (d.r == n

我有在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 { }
    }
但在调试模式中,当我检查一个项目并查看
ChkLiboData.CheckedItems
时,我在
ChkLiboData.CheckedItems
中看不到任何项目。
我该怎么办?

当项目的已检查状态即将更改时,会引发事件
ItemCheck
。它还没有改变。让我用一个例子来说明这一点。CheckedListBox包含3项“A”、“B”和“C”。未检查任何项目。现在,用户检查项目“A”。触发事件
ItemCheck
。属性
CheckedItems
不包含任何项。在事件args
e
(类型为
ItemCheckEventArgs
)中,您可以找到检查状态正在更改的项目的索引、包含之前检查状态的属性
CurrentValue
,以及包含新检查状态的属性
NewValue
。如果用户随后检查项目“B”,则再次触发事件。这次属性
CheckedItems
包含一项“A”。a、 s.o


顺便说一句:您可以在ItemCheck事件中设置属性
NewValue
。这为您提供了一种可能性,例如防止检查项目。

当项目的检查状态即将更改时,会引发事件
ItemCheck
。它还没有改变。让我用一个例子来说明这一点。CheckedListBox包含3项“A”、“B”和“C”。未检查任何项目。现在,用户检查项目“A”。触发事件
ItemCheck
。属性
CheckedItems
不包含任何项。在事件args
e
(类型为
ItemCheckEventArgs
)中,您可以找到检查状态正在更改的项目的索引、包含之前检查状态的属性
CurrentValue
,以及包含新检查状态的属性
NewValue
。如果用户随后检查项目“B”,则再次触发事件。这次属性
CheckedItems
包含一项“A”。a、 s.o


顺便说一句:您可以在ItemCheck事件中设置属性
NewValue
。这使您有可能(例如)阻止检查某个项目。

可能重复的@Ahmadalishafie:我已根据您的评论更改了我的答案。@Ahmadalishafie:我已根据您的评论更改了我的答案。