Controls.Remove(control)不起作用? [英] Controls.Remove(control) doesn't work?

查看:349
本文介绍了Controls.Remove(control)不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前遇到过这个问题,我的解决方案是调用Controls.Remove(control)的while循环,直到Controls.Contains(control)返回false.当仅通过一次调用Controls.Remove(control)来完成该操作时,这太奇怪了.

我有一个带有某些控件的表单,我想遍历其所有控件,并使用myForm.Controls.Remove(control)检查一些符合某些条件的控件,以便从表单中删除它,它应该可以工作,但是只有一些控件可以已删除,并且其他符合我希望也要删除的条件的控件仍在表单上. myForm.Controls.Clear()可以完美运行,但这不是我想要的.

我不明白为什么会这样.如果您不相信,则可以通过向表单中随机添加一些控件,然后尝试使用Controls.Remove()从表单中删除其中一些控件来轻松地进行测试.我尝试过包括删除SuspendLayout和ResumeLayout之间的控件的代码块,但是它仍然无法正常工作.您对这个问题有什么线索吗?你能帮我吗?
我想说的是,您应该使用for循环进行测试以删除控件,删除每个控件似乎已成功完成.问题似乎是for循环的运行速度如此之快,以致于该窗体无法以该速度删除其控制.我想这肯定是一个错误.

这是我的测试代码:

 公共 Form1()
        {
            InitializeComponent();
            随机兰特=  Random();
             for ( int  i =  0 ; i <   30 ; i ++)
            {
                标签lbl =  Label();
                lbl.Text = i.ToString();
                lbl.Location = 点(rand.Next( 10  1000 ),rand.Next( 10  1000 )));
                Controls.Add(lbl);
            }
            点击+ =(发送方,e)= > 
            {
                 foreach (控件C  控件中的)
                {
                    Controls.Remove(C); // 在不满足任何条件的情况下删除
                }
            };
        } 


感谢您的帮助!
谢谢!

解决方案

我想问题出在试图枚举集合时弄乱了集合.

我测试了以下内容,它似乎可以工作:

 同时(Controls.Count >   0 );
} 



这是完整的代码:

 公共  class  Form1:表单
    {
        公共 Form1()
        {
            
            随机兰特=  Random();
             for ( int  i =  0 ; i <   30 ; i ++)
            {
                标签lbl =  Label();
                lbl.Text = i.ToString();
                lbl.Location = 点(rand.Next( 10  1000 ),rand.Next( 10  200 )));
                Controls.Add(lbl);
            }

            .MouseDoubleClick+ =
                 MouseEventHandler(Form1_MouseDoubleClick);

        }

        无效 Form1_MouseDoubleClick(对象发​​件人,MouseEventArgs e)
        {

            同时(Controls.Count >   0  )
            {
                Controls.RemoveAt( 0 );
            }

            //  foreach(控件中的控件C)
            //  {
            //  Controls.Remove(C);//在不满足任何条件的情况下删除
            // } 
        }
    } 


我建议您一次性进行过滤(将引用复制到数组),然后在过滤后的控件中调用Controls.Remove(C):
运行foreach循环.

  foreach (控件C  in 中的filteredControls)
{
    Controls.Remove(C);
} 


糟糕!
这不是一个好主意-如果有多个控件可用,您应该得到一个例外.

问题在于,遍历集合时无法更改集合,就像在foreach循环中尝试这样做一样.

如果您没有(也没有)获得异常,则只能是Controls属性每次都不返回相同的集合-因此会出现一些延迟或其他任务怪异,这使问题感到困惑. /blockquote>

Hi, I''ve encountered this problem before and my solution was a while loop of calling Controls.Remove(control) until Controls.Contains(control) returns false. That''s too odd when the thing should be done in only one call to Controls.Remove(control).

I have a form with some controls on it, I want to loop through all its controls and check for some controls meeting some criteria to remove from the form using myForm.Controls.Remove(control), it should work, but only some controls are removed and other controls meeting the criteria that I expected to be also removed are still on the form. The myForm.Controls.Clear() works perfectly but that''s not I want.

I can''t understand why that happens. If you don''t believe, you can test this easily by adding randomly some controls to your form and then try removing some of them from the form with Controls.Remove(). I''ve tried including the block of code removing the controls between SuspendLayout and ResumeLayout but it still doesn''t work. Do you have any clue on this issue? Could you please help me out?
I want to say that you should test with a for loop to remove the controls, removing each control seems to be done successfully. The problem seems to be the for loop works so fast that the form couldn''t remove its control by that speed. This is surely a bug, I think so.

Here is my testing code:

public Form1()
        {
            InitializeComponent();
            Random rand = new Random();
            for (int i = 0; i < 30; i++)
            {
                Label lbl = new Label();
                lbl.Text = i.ToString();                
                lbl.Location = new Point(rand.Next(10, 1000), rand.Next(10, 1000));
                Controls.Add(lbl);
            }
            Click += (sender, e) =>
            {
                foreach (Control C in Controls)
                {
                    Controls.Remove(C);//Remove without meeting any criteria
                }
            };
        }


Your help should be highly appreciated!
Thanks!

解决方案

I suppose the problem arises from messing up with the collection while trying to enumerate it.

I tested with the following and it seems to work:

while (Controls.Count > 0)
{
    Controls.RemoveAt(0);
}



Here''s the full code:

public class Form1 : Form
    {
        public Form1()
        {
            
            Random rand = new Random();
            for (int i = 0; i < 30; i++)
            {
                Label lbl = new Label();
                lbl.Text = i.ToString();
                lbl.Location = new Point(rand.Next(10, 1000), rand.Next(10, 200));
                Controls.Add(lbl);
            }

            this.MouseDoubleClick += 
                new MouseEventHandler(Form1_MouseDoubleClick);

        }

        void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            while (Controls.Count > 0)
            {
                Controls.RemoveAt(0);
            }

            //foreach (Control C in Controls)
            //{
            //    Controls.Remove(C);//Remove without meeting any criteria
            //}
        }
    }


I suggest you do the filtering in one go (copying the references to an array) and then run a foreach loop in the filtered controls calling Controls.Remove(C):

foreach (Control C in filteredControls)
{
    Controls.Remove(C);
}


Ouch!
Not a good idea - you should get an exception if there is more than one control available.

The problem is that you cannot alter a collection while you are iterating through it, as you are trying to do in your foreach loop.

If you do not get an exception (and you don''t) it can only be that the Controls property does not return the same collection each time - so there is some delay or other tasking oddity going on which is confusing the issue.


这篇关于Controls.Remove(control)不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆