foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
this.Controls.Remove(c);
}
}
我是动态生成多个checkbox,好比生成了10个,然后我想删除这些checkbox,就用上面的方法,可是每次执行这个方法,都不能完全删除这10个checkbox,只能删除3,4个,然后再执行,它再删除3,4个,这是为什么??
{
if (c is CheckBox)
{
this.Controls.Remove(c);
}
}
我是动态生成多个checkbox,好比生成了10个,然后我想删除这些checkbox,就用上面的方法,可是每次执行这个方法,都不能完全删除这10个checkbox,只能删除3,4个,然后再执行,它再删除3,4个,这是为什么??
解决方案 »
- 如何模拟提交页面,并且上传附件?
- 我有一个动画,想让它反着执行?
- 怎么将这种数据写个循环。。。。。。。。。。。
- winForm中的Button怎么赋值?
- (散分)请问AxWebBrowser怎么打开一个本地的HTML文件???
- 用nHibernate开发winform时出现的The dialect was not set 的问题,向高手求教。
- 请教一个简单问题,数据类型 string 和 String 的区别在哪里?
- 如何获取本机机器码,地区语言
- 如何用datagrid显示统计数据
- 开发中关于权限验证
- DataGrid如何绑定DropDownList ?在线求解!!
- UDP客户端怎样得知服务器关闭或出异常信息
执行时,不能改变Controls的大小,你在循环中Remove会让Controls变小
你可以这样:
List<Control> list=new List<Control>();
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
list.Add(c);
}
}
foreach(Control c in list)
{
this.Controls.Remove(c);
}
try thisList<CheckBox> checkboxList = new List<CheckBox>();
foreach (Control c in Controls)
{
if (c is CheckBox) checkboxList.Add(c as CheckBox);
}
foreach (CheckBox c in checkboxList)
{
Controls.Remove(c);
}
this.Controls.Cast<Control>().Where(c=>c is CheckBox).ToList().ForEach(c => Controls.Remove(c));