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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询