【问题标题】:Checked List Box's Checked Items doesn't work选中列表框的选中项目不起作用
【发布时间】:2011-12-18 20:52:44
【问题描述】:

我有在 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 中看不到任何项目。 我该怎么办???

【问题讨论】:

标签: c# .net winforms c#-4.0


【解决方案1】:

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

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

【讨论】:

  • @ahmadalishafiee:我已经根据你的评论改变了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
相关资源
最近更新 更多