0

Hypocritically speaking I have a job that is creating ideas for my clients. For one client I have a list of suggestions, from which I created checkboxs accordingly. Now I need to know which of those are unchecked by the client, so I can delete them.

-The number of suggestions is not fixed. The number is the length of my current SuggestionList.

int checkboxnumber = Myclass.suggestionline.Count();

for (int i = 0; i < checkboxnumber; i++)
{
    CheckBox cb = new CheckBox();
    cb.Text = Myclass.suggestionline[i][0];
    cb.Location = new Point(5, 5 + i * 24);
    cb.BackColor = Color.White;
    cb.Name = "checkbox"+i;
    cb.AutoSize = true;
    cb.Checked = true;
    panel1.Controls.Add(cb);
};

I structured my SuggestionList as List < List < string > >, a 4 suggestions SuggestionList example would be :

{{"suggestion1", "like", "100 Euro"},{"suggestion2", "like", "200 Euro"},{"suggestion3", "like", "300 Euro"},{"suggestion4", "like", "400 Euro"}}

JSteward
  • 6,833
  • 2
  • 21
  • 30
Kaidi G
  • 43
  • 4

1 Answers1

0

I figured it out. Thanks to this related question: How can I create a dynamic button click event on a dynamic button?

CheckBox cb = new CheckBox();

...

int checkboxnumber = Myclass.suggestionline.Count();

        for (int i = 0; i < checkboxnumber; i++)
        {
            cb = new CheckBox();
            cb.Text = Myclass.suggestionline[i][0];
            cb.Location = new Point(5, 5 + i * 24);
            cb.BackColor = Color.White;
            cb.Name = "checkbox"+i;
            cb.AutoSize = true;
            cb.Checked = true;
            cb.CheckedChanged += new EventHandler(CheckedChanged);
            panel1.Controls.Add(cb);
        };

...

private void CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if (!cb.Checked)
            {
                Console.WriteLine(cb.Text);

            }
        }
Kaidi G
  • 43
  • 4