foreach (Control  obj5 in this.Controls )//判断界面上的控件            {
                 if (obj5.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
                 {
                       //此时只要执行你的删除控件代码即可
                 }
            }

解决方案 »

  1.   


    private void delLabel(control con){
        foreach (Control  obj in con.Controls ){
            if (obj.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable 
            { 
                          //此时只要执行你的删除控件代码即可 
             }
            delLabel(obj);
    }
    }调用时传this进去就行了
      

  2.   


    这是因为在foreach遍历时,删除控件,使得this.controls发生了变化.少了数据元素,结果只能删除一半.
      

  3.   

    解决方法:for(int i=0;i<this.Controls.Count;i++)
    {//注意:Controls中的控件是动态变化的.
        foreach(Control con in this.Controls)
        {
            if(con is Label)
                this.Controls.Remove(con);
         }
    }
      

  4.   

    问题已经解决,跟大家共享,其实楼上几位的也不能解决问题,我刚刚也是按照那个方法~~~      public static void RemoveControl(Control c,string R_name)
            {
                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]);
                }
            }
      

  5.   

    来晚了,这种情况就是用循环倒着来就可以了,用foreach也没办法解决成员被删除后的指针重定位
      

  6.   

      for(int i=0  i< this.Controls.Count;i++ )//判断界面上的控件 
               {
    Control obj5=this.Controls[i];                if (obj5.GetType.ToString() = "System.Windows.Forms.Label") //如果控件是lable
                    {
    this.Controls.Remove(i);
    i--;
                          //此时只要执行你的删除控件代码即可
                    }
                }
      

  7.   

    最简单的代码来了
    ====================================
     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--;
                    }
                
                }
            }
      

  8.   

    是不是这年头for循环只用来递增了?for (int i = this.Controls.Count - 1; i >= 0; i--)
    {
        if (this.Controls[i] is Label)
        {
            this.Controls.RemoveAt(i);
        }
    }