【转载】foreach+Control.Controls无法一次性移除所有子控件解决方法
博客转载地址: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无法一次性移除所有子控件解决方法的更多相关文章
- swift 移除所有子控件
/// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...
- (转载) Android RecyclerView 使用完全解析 体验艺术般的控件
Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏 ...
- [转载]ExtJs4 笔记(8) Ext.slider 滚轴控件、 Ext.ProgressBar 进度条控件、 Ext.Editor 编辑控件
作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...
- Xaml Controls Gallery 的五个没有用的控件
HyperlinkButton 功能:这个控件可以实现点击按钮后跳到另一个按钮的功能. 我觉得这个功能有些多余,据我了解,一些深受欢迎的游戏大都具备的一个特点,那就是操作简单,界面中不会出现冗余的东西 ...
- 【转载】Android RecyclerView 使用完全解析 体验艺术般的控件
崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...
- [转载]mysqlcreate新建用户host使用%,本地无法连接原因及解决方法
转载自 http://www.2cto.com/database/201307/225781.html mysql,因为root权限过高,所以新建一用户appadmin,权限仅为要用到的数据库.创建语 ...
- (转载)VC/MFC 工具栏上动态添加组合框等控件的方法
引言 工具条作为大多数标准的Windows应用程序的 一个重要组成部分,使其成为促进人机界面友好的一个重要工具.通过工具条极大方便了用户对程序的操作,但是在由Microsoft Visual C++开 ...
- 转载:Android RecyclerView 使用完全解析 体验艺术般的控件
转自:https://blog.csdn.net/lmj623565791/article/details/45059587
- 转载: 开源整理:Android App新手指引开源控件
http://blog.coderclock.com/2017/05/22/android/open-source-android-app-guide-view-library/ 开源整理:Andro ...
随机推荐
- 基于request.getAttribute与request.getParameter的区别详解
HttpServletRequest类既有getAttribute()方法,也有getParameter()方法,这两个方法有以下区别:1.HttpServletRequest类有setAttribu ...
- greenlet 实现手动协程切换
from greenlet import greenlet def test1(): print('12') gr2.switch() #切换到gr2 print('34') gr2.switch() ...
- Spring MVC 支持的原生API参数
HttpServletRequest HttpServletResponse HttpSession java.security.Principal Local InputStream OutputS ...
- 文字折行不折行 css
white-space : 1. normal 默认值 ,文字自动换行. 2. pre 使用<pre>标签形式,表示元素. * ...
- Qt 的事件
一个事件由一个特定的QEvent子类来表示,如QMouseEvent.QKeyEvent 处理一个事件的方法: 方法一:重新实现部件的paintEvent.mousePressEvent等事件处理函数 ...
- 2018.8.14-C#复习笔记总
using System; using System.Collections.Generic; //using System.Linq; using System.Text; using System ...
- Haskell语言学习笔记(65)Data.HashMap
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
- MySQL中实现Oracle里面 rank()over ( PARTITION BY ORDER BY) 分类分组功能
各班级学生成绩测试表 select * from TMP_A; 实现目的: 按照班级分类后按照分数倒序排序 采用MySQL变量简单实现,SQL如下: SELECT a.stu_id,a.point, ...
- Anaconda中python加入环境变量
1.我的电脑---高级系统设置 2.选中环境变量,保存. 3.在系统环境变量PATH中,加入Anaconda3及Script路径加入其中 4.测试python
- webdriver自动化脚本
1.需求如下: 启动火狐浏览器首先打开百度,等待3秒然后打开博客首页,等待2秒然后关闭浏览器 from selenium import webdriver from time import sleep ...