Search code examples
c#winformschecklistbox

How do I create a Winform checklist box with multiple Items in it, pared with Textboxes for user Data Entry?


I need a control which I can iterate through and change the items in it based on which option is selected in a separate combobox. Any direction would be helpful, thank you!.

Example

I can place each element separately, but then I'm not sure how to do that iteratively without a lot of work. I'm wondering if there is a better control for me to use.


Solution

  • An easy way to achieve this is to use the DataGridView control and add sufficient DataGridViewColumn. In your example you could add DataGridViewCheckBoxColumn and DataGridViewTextBoxColumn to your grid view. The only downside is that (at least to my knowledge) there is no easy way to add text directly to the checkbox cell but instead one could add another DataGridViewTextBoxColumn to display that information.

    How it would look like:

    Image

    Just add a DataGridView in your designer then explicitly add three columns to it. In my example the first and third column is of type DataGridViewTextBoxColumn whilst the middle column is of type DataGridViewCheckBoxColumn.

    Code

     private void button1_Click(object sender, EventArgs e)
            {
                addRow("Pavement", -0.25);
                addRow("End of way", 0);
                addRow("ELER", 0);
                addRow("ELEL", 0);
                addRow("ELEN", 0);
                addRow("ELEB", 0);
    
            }
    
     private void addRow(string pointCode, double offset)
            {
                DataGridViewTextBoxCell pcc = new DataGridViewTextBoxCell();
                DataGridViewCheckBoxCell cbc = new DataGridViewCheckBoxCell();
                DataGridViewTextBoxCell ofc = new DataGridViewTextBoxCell();
                pcc.Value = pointCode;
                ofc.Value = offset.ToString();
                cbc.Value = true; //Checkbox marked
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(pcc);
                row.Cells.Add(cbc);
                row.Cells.Add(ofc);
                dataGridView1.Rows.Add(row);
            }