2017-06-09 64 views
0

我有一个复选框列表,我想要获取所有选定的项目并将选定的文本分配给字符串。我不明白为什么我得到错误复选框列表选中的方法不起作用

“错误1‘对象’不包含‘经过’的定义,并没有扩展方法‘经过’接受型‘对象’的第一个参数可以发现(是否缺少using指令或程序集引用?”

 for (int i = 0; i < checkedListBoxA.Items.Count - 1; i++) 
     { 
      if (checkedListBoxA.Items[i].Checked==1) 
      { 
       SelectedIt += checkedListBoxA.Items[i].Text + "<br />"; 
      } 
     } 
    } 
+1

checkedListBox1所有选定的项目不会吧更容易使用[CheckedListBox.CheckedItems属性](https://msdn.micr osoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems(v=vs.110).aspx) - _“此CheckedListBox中选中项目的集合。”_? – stuartd

+0

Checked没有被识别的原因是什么? – Elias

+0

是的,如果您看[文档](https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.item(v = vs.110).aspx),返回类型是“Object”。 – stuartd

回答

1

使用的foreach

foreach (object itemChecked in checkedListBox1.CheckedItems) 
{     
    textBox1.Text += itemChecked.ToString() + " "; 
} 

或使用For循环

for(int i = 0; i<checkedListBox1.Items.Count; i++) 
{ 
    for(int j = 0; j<checkedListBox1.CheckedItems.Count; j++) 
    { 
      if(checkedListBox1.Items[i] == checkedListBox1.CheckedItems[j]) 
      { 
       textBox1.Text += checkedListBox1.Items[i].ToString() + " "; 
      } 
    }     
} 

这两个代码将打印在textBox1

0

好吧,我使用的foreach,但我就是不明白,为什么上面的解决方案没有奏效。`

String SelectedIt = ""; 


      foreach (int indexChecked in checkedListBoxA.CheckedIndices) 
      { 


       SelectedIt += checkedListBoxA.Items[indexChecked].ToString() +", "; 

      } 

`