C# 动态生成的多个checkbox,如何把选择的项的Text取出来放到变量内

如题所述

public Form1()
{
    InitializeComponent();
    CheckBox check;
    for (int i = 0; i < 10; i++)
    {
        check = new CheckBox();
        check.Text = i.ToString();
        check.Location = new Point(0, i * 25);
        check.CheckedChanged += new EventHandler(check_CheckedChanged);
        this.Controls.Add(check);
    }
}
List<string> checkBoxTextList = new List<string>();
void check_CheckedChanged(object sender, EventArgs e)
{
    CheckBox checkbox = sender as CheckBox;
    if (checkbox.Checked)
    {
        if (!checkBoxTextList.Contains(checkbox.Text))
        {
            checkBoxTextList.Add(checkbox.Text);
        }
    }
    else
    {
        if (checkBoxTextList.Contains(checkbox.Text))
        {
            checkBoxTextList.Remove(checkbox.Text);
        }
    }
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网