foreach (Control obj5 in this.Controls )//判断界面上的控件 {
if (obj5.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
{
//此时只要执行你的删除控件代码即可
}
}
if (obj5.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
{
//此时只要执行你的删除控件代码即可
}
}
private void delLabel(control con){
foreach (Control obj in con.Controls ){
if (obj.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
{
//此时只要执行你的删除控件代码即可
}
delLabel(obj);
}
}调用时传this进去就行了
这是因为在foreach遍历时,删除控件,使得this.controls发生了变化.少了数据元素,结果只能删除一半.
{//注意:Controls中的控件是动态变化的.
foreach(Control con in this.Controls)
{
if(con is Label)
this.Controls.Remove(con);
}
}
{
int i = 0;
object[] obj = new object[ControlCount(c, R_name)];
foreach (Control ch in c.Controls)
{
if ((ch is Label))
{
if (ch.Name.Substring(0, 2) == R_name)
{
obj[i] = ch;
i++;
}
}
}
for (int x = 0; x < obj.Length; x++)
{
c.Controls.Remove((Label)obj[x]);
}
}
{
Control obj5=this.Controls[i]; if (obj5.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
{
this.Controls.Remove(i);
i--;
//此时只要执行你的删除控件代码即可
}
}
====================================
private void button6_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count; i++)
{
if (Controls[i] is Label)
{
this.Controls.Remove(Controls[i]);
i--;
}
}
}
{
if (this.Controls[i] is Label)
{
this.Controls.RemoveAt(i);
}
}