C#:永远循环使用CheckedListBox项

时间:2011-06-23 19:44:53

标签: c#

foreach (CheckedListBox item in itemInfoCheckList.Items)
            {
                if (item.CheckState == CheckState.Checked)
                    SW.WriteLine(item.Text + " :  YES");
                else
                    SW.WriteLine(item.Text + " : NO");
            }

上面的代码片段是它循环的地方......虽然只有2个项目 以下是iteminfochecklist定义

 this.itemInfoCheckList.CheckOnClick = true;
        this.itemInfoCheckList.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.itemInfoCheckList.FormattingEnabled = true;
        this.itemInfoCheckList.Items.AddRange(new object[] {
        "item 1 ",
        "item 2"});
        this.itemInfoCheckList.Location = new System.Drawing.Point(573, 350);
        this.itemInfoCheckList.Name = "itemInfoCheckList";
        this.itemInfoCheckList.Size = new System.Drawing.Size(197, 38);
        this.itemInfoCheckList.TabIndex = 143;

2 个答案:

答案 0 :(得分:2)

而不是这段代码

foreach (CheckedListBox item in itemInfoCheckList.Items)

使用此代码

foreach (Object item in itemInfoCheckList.CheckedItems)

答案 1 :(得分:0)

还可以像这样使用方法GetItemChecked和SetItemChecked:

/api/account.py