Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个项目的复选框_C#_Checkbox - Fatal编程技术网

C# 每个项目的复选框

C# 每个项目的复选框,c#,checkbox,C#,Checkbox,我对检查列表框失去了理智,我试图获取列表中所有已检查的项目。下面是我使用的代码,带有我的用户界面上的按钮: public partial class formPCRBaseline : Form { public formPCRBaseline(List<GetBaselineSectionTasks> m_objPCRCheck) { InitializeComponent(); setDefaults(m_objPCRCheck)

我对
检查列表框
失去了理智,我试图获取列表中所有已检查的项目。下面是我使用的代码,带有我的用户界面上的按钮:

 public partial class formPCRBaseline : Form
{
    public formPCRBaseline(List<GetBaselineSectionTasks> m_objPCRCheck)
    {
        InitializeComponent();
        setDefaults(m_objPCRCheck);

    }

    private void setDefaults(List<GetBaselineSectionTasks> m_objPCRCheck)
    {
        checkedListBox.BackColor = Color.White;
        List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks>();
        int i= 0;
        foreach (GetBaselineSectionTasks i_objPCRCheck in m_objPCRCheck)
        {
            checkedListBox.Items.Add(i_objPCRCheck.taskname);



        }

    }

    private void buttonConfirm_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();

    }
    private void buttonClose_Click_1(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }

    public List<GetBaselineSectionTasks> GetCheckedItems()
    {
        List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks>();
        m_objCheckeditem.AddRange(checkedListBox.CheckedItems.OfType<GetBaselineSectionTasks>());




        return m_objCheckeditem;
    }


}
}
但它不起作用,当我使用断点时,它到达了
foreach
,我有两个选定项,但它没有进入 你知道我能做什么吗?

你在找房子吗?假设
CheckListBox
-
checkedListBox
包含
GetBaselineSectionTasks
项,即您这样填充它:

  checkedListBox.Items.Add(new GetBaselineSectionTasks(...));
  ... 
  checkedListBox.Items.Add(new GetBaselineSectionTasks(...));
 checkedListBox.Items.Add("Some Value");
 ... 
 checkedListBox.Items.Add("Some other value");
你可以把

  List<GetBaselineSectionTasks> m_objCheckeditem = new List<GetBaselineSectionTasks();

  ...
 m_objCheckeditem.AddRange(checkedListBox.CheckedItems.OfType<GetBaselineSectionTasks>());
您可以循环检查项目,并在尝试输出相应的

 foreach (int index in checkedListBox.CheckedIndices) {
   string taskName = Convert.ToString(checkedListBox[index]);

   // Now you have item index and item text (which is taskName). Let's try filling the list
   // We can try finding the item by string
   for (var item in m_objPCRCheck) {
     if (item.taskname == taskName) {
       m_objCheckeditem.Add(item);

       break; 
     } 
   }

   // Or (alternatively) you can trying to get index-th item:
   // m_objCheckeditem.Add(m_objPCRCheck[index]);
 }

windows应用程序ID您是否尝试通过谷歌搜索如何从checklistbox访问选中的项目?是的,但它不工作:/你能定义
不工作吗?
CheckedListBox.CheckedItemCollection
?它不工作,没有在我的列表中添加任何内容这很奇怪,因为如果我放置断点,我会看到我在复选框列表中选择的所有项目,但这些项目不会添加到我的列表中list@Cookistador:列表中有哪些项目<代码>字符串
s,
GetBaselineSectionTask
还有什么吗?只有两个字符串,一个是taskname,一个是taskID@Cookistador:那么您将
选中列表框
填充为
选中列表框。添加(“SomeTaskName”)
,对吗?我编辑代码是为了向您展示如何填充我的选中列表框
 checkedListBox.Items.Add("Some Value");
 ... 
 checkedListBox.Items.Add("Some other value");
 foreach (int index in checkedListBox.CheckedIndices) {
   string taskName = Convert.ToString(checkedListBox[index]);

   // Now you have item index and item text (which is taskName). Let's try filling the list
   // We can try finding the item by string
   for (var item in m_objPCRCheck) {
     if (item.taskname == taskName) {
       m_objCheckeditem.Add(item);

       break; 
     } 
   }

   // Or (alternatively) you can trying to get index-th item:
   // m_objCheckeditem.Add(m_objPCRCheck[index]);
 }