dimanche 9 septembre 2018

How to store a checkbox object in a list?

I'm struggle with the CheckBox in C#

 public formPCRBaseline(List<GetBaselineSectionTasks> m_objPCRCheck)
 {
     setDefaults(m_objPCRCheck);
 }

 private void setDefaults(List<GetBaselineSectionTasks> m_objPCRCheck)
 {
     richTextBoxPCRBaseline.BackColor = Color.White;

     foreach (GetBaselineSectionTasks i_objPCRCheck in m_objPCRCheck)
     {
         richTextBoxPCRBaseline.SelectionIndent = 0;
         CheckBox checkBox = new CheckBox();
         checkBox.Height = 20;
         checkBox.Width = 20;
         richTextBoxPCRBaseline.Controls.Add(checkBox);
         checkBox.Tag = i_objPCRCheck;
         richTextBoxPCRBaseline.SelectionIndent = 30;
         richTextBoxPCRBaseline.SelectedText = "Section : '" + i_objPCRCheck.taskname;
         richTextBoxPCRBaseline.SelectedText = "\n\n";
     }
}

So this code return me a list of object with some checkbox, and I'd like to take the checked checkbox when I clicked on confirm button, so I thought to do something like:

private void buttonConfirm_Click(object sender, EventArgs e, out List<GetBaselineSectionTasks> m_objCheckeditem)
{
    int i = 0;
    m_objCheckeditem = new List<GetBaselineSectionTasks>();
    foreach (CheckBox i_objCheck in richTextBoxPCRBaseline.Controls)
    {
        if (!i_objCheck.Checked)
        {
            m_objCheckeditem.Add(i_objCheck.Tag);
            MessageBox.Show(i_objCheck.Tag.ToString());
        }
        i++;
    }
}    

It's the first time that I use the .Tag property so I suggest that it's not working like that.

Any idea of how could I get my checked item and return them?




Aucun commentaire:

Enregistrer un commentaire