博客转载地址:http://www.mzwu.com/article.asp?id=2254

//在panel1中添加20个Button
int row = -;
int rowcount = ; for (int i = ; i <= ; i++)
{
if (i % rowcount == )
row++; Button button = new Button();
button.Width = ;
button.Height = ;
button.Text = "button" + i.ToString();
button.Top = row * ( + );
button.Left = ((i-) % rowcount) * ( + ); panel1.Controls.Add(button);
}

使用foreach+Control.Controls移除所有Button:

foreach (Control con in panel1.Controls)
panel1.Controls.Remove(con);

结果发现只移除了10个,界面还剩下10个!改用for循环:

for (int i = ; i < panel1.Controls.Count; i++)
panel1.Controls.RemoveAt(i);

结果同上,正确的代码应为:

for (int i = panel1.Controls.Count - ; i >= ; i--)
panel1.Controls.RemoveAt(i);

究其原因,应是调用Remove或RemoveAt方法时,也改变了Controls内容所致,做个测试:

for (int i = ; i < panel1.Controls.Count; i++)
{
textBox1.Text += string.Format("i值:{0}, Controls.Count值:{1}, 移除:{2}\r\n", i, panel1.Controls.Count, panel1.Controls[i].Text);
panel1.Controls.RemoveAt(i);
}

结果:

i值:, Controls.Count值:, 移除:button1
i值:, Controls.Count值:, 移除:button3
i值:, Controls.Count值:, 移除:button5
i值:, Controls.Count值:, 移除:button7
i值:, Controls.Count值:, 移除:button9
i值:, Controls.Count值:, 移除:button11
i值:, Controls.Count值:, 移除:button13
i值:, Controls.Count值:, 移除:button15
i值:, Controls.Count值:, 移除:button17
i值:, Controls.Count值:, 移除:button19

测试发现:移除子控件应尽量用Remove或RemoveAt方法,若使用Clear方法,在多次动态添加/移除控件后,程序占用内存越来越高,估计是Clear没有及时释放资源所致。 

......

【转载】foreach+Control.Controls无法一次性移除所有子控件解决方法的更多相关文章

  1. swift 移除所有子控件

    /// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...

  2. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

  3. [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  4. Xaml Controls Gallery 的五个没有用的控件

    HyperlinkButton 功能:这个控件可以实现点击按钮后跳到另一个按钮的功能. 我觉得这个功能有些多余,据我了解,一些深受欢迎的游戏大都具备的一个特点,那就是操作简单,界面中不会出现冗余的东西 ...

  5. 【转载】Android RecyclerView 使用完全解析 体验艺术般的控件

    崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...

  6. [转载]mysqlcreate新建用户host使用%,本地无法连接原因及解决方法

    转载自 http://www.2cto.com/database/201307/225781.html mysql,因为root权限过高,所以新建一用户appadmin,权限仅为要用到的数据库.创建语 ...

  7. (转载)VC/MFC 工具栏上动态添加组合框等控件的方法

    引言 工具条作为大多数标准的Windows应用程序的 一个重要组成部分,使其成为促进人机界面友好的一个重要工具.通过工具条极大方便了用户对程序的操作,但是在由Microsoft Visual C++开 ...

  8. 转载:Android RecyclerView 使用完全解析 体验艺术般的控件

    转自:https://blog.csdn.net/lmj623565791/article/details/45059587

  9. 转载: 开源整理:Android App新手指引开源控件

    http://blog.coderclock.com/2017/05/22/android/open-source-android-app-guide-view-library/ 开源整理:Andro ...

随机推荐

  1. 28. 表单css样式定义格式

    form>table>tbody>tr>td{padding:5px;font-size:14px;font-family:"Microsoft YaHei" ...

  2. 24. (ora-01410无效的rowid)临时表 on commit delete rows 与 on commit preserve rows 的区别

    ora-01410无效的rowid解决方式: 把临时表空间改成会话级别的就可以了,即把临时表的创建选项由on commit delete rows改为on commit preserve rows,就 ...

  3. springboot - SqlSessionFactoryBean falls in circular dependencies by Spring Boot's DataSourceInitializer

    springboot mybatis配置多数据源的时候,报错:There is a circular dependency between 7 beans in the application con ...

  4. WP8.1 控件默认字体颜色 配置文件位置

    C:\Program Files (x86)\Windows Phone Kits\8.1\Include\abi\Xaml\Design\generic.xaml 可在App.xaml文件中over ...

  5. 自动选择最佳特征进行分类-SVM (Halcon)

    HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...

  6. mongodb基础学习5-索引

    下面来看看索引,有btree索引和hash索引,会提高查询速度,但降低了写入速度,可以按升,降序建立 包括单列索引,多列索引,子文档索引,也可分为普通索引,惟一索引,稀疏索引,hash索引(2.4新增 ...

  7. 电影TS、TC、BD版和HD版

    HD的意思是指HDTV,HDTV指网上下载的高清影片,它的画面品质会比BD稍差,主要表现为亮度不足,色彩不自然等.BD是指蓝光(Blu-ray)或称蓝光盘(Blu-ray Disc,缩写为BD),目前 ...

  8. 吴裕雄 数据挖掘与分析案例实战(2)——python数据结构及方法、控制流、字符串处理、自定义函数

    list1 = ['张三','男',33,'江苏','硕士','已婚',['身高178','体重72']]# 取出第一个元素print(list1[0])# 取出第四个元素print(list1[3] ...

  9. 分类模型评估之ROC-AUC曲线和PRC曲线

    http://blog.csdn.net/pipisorry/article/details/51788927 在样本分布及其不均匀的情况下,建议用PRC...可以看下这个精确率.召回率.F1 值.R ...

  10. scala 建模

    // train multinomial logistic regression val lr = new LogisticRegressionWithLBFGS() .setIntercept(tr ...