C#如何找出动态创建的不确定数量的复选框的任何checkstate更改

时间:2018-03-05 22:05:12

标签: c# windows winforms checkbox dynamic

虚伪地说,我有一份工作正在为我的客户创造想法。 对于一个客户端,我有一个建议列表,我从中创建了相应的复选框。 现在我需要知道客户端中哪些未经检查,所以我可以删除它们。

- 建议数量不固定。该数字是我当前 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);
};

我将SuggestionList构建为 List&lt;列表&lt;字符串&gt; &gt; ,4个建议SuggestionList示例如下:

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

1 个答案:

答案 0 :(得分:0)

我明白了。感谢您提出的相关问题: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);

            }
        }