2011-06-23 115 views
2
foreach (CheckedListBox item in itemInfoCheckList.Items) 
      { 
       if (item.CheckState == CheckState.Checked) 
        SW.WriteLine(item.Text + " : YES"); 
       else 
        SW.WriteLine(item.Text + " : NO"); 
      } 

上面的代碼片段是它循環......雖然只有2項 下面是iteminfochecklist定義C#:循環永遠的foreach CheckedListBox項目

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; 
+3

請向我們展示定義/填充itemInfoCheckList的代碼。僅憑循環就不可能做任何事情。 –

+2

問題在哪裏? – Reniuz

+3

該代碼不會進入無限循環。你怎麼確定它是? –

回答

2

取而代之的是代碼

foreach (CheckedListBox item in itemInfoCheckList.Items) 

使用此代碼

foreach (Object item in itemInfoCheckList.CheckedItems)