C# : Looping forever foreach CheckedListBox item
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));
}
精彩评论