开发者

C# : Looping forever foreach CheckedListBox item

开发者 https://www.devze.com 2023-03-15 12:51 出处:网络
foreach (CheckedListBox item in itemInfoCheckList.Items) { if (item.CheckState == CheckState.Checked) SW.WriteLine(item.Text + \" :YES\");
foreach (CheckedListBox item in itemInfoCheckList.Items)
            {
                if (item.CheckState == CheckState.Checked)
                    SW.WriteLine(item.Text + " :  YES");
                else
                    SW.WriteLine(item.Text + " : NO");
            }

THe above code snippet is where it loops ... although there are only 2 items Below is the iteminfochecklist definition

 this.itemInfoCheckList.CheckOnClick = true;
        this.itemInfoCheckList.Font = new开发者_JAVA技巧 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;


Instead of this code

foreach (CheckedListBox item in itemInfoCheckList.Items)

use this code

foreach (Object item in itemInfoCheckList.CheckedItems)


There's also the methods GetItemChecked and SetItemChecked you can use like so:

CheckedListBox clb = (CheckedListBox)sender;
foreach (string item in clb.Items)
{
    bool isChecked = clb.GetItemChecked(clb.FindStringExact(item));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消