C# 控件。删除仅删除其他所有控件

C# 控件。删除仅删除其他所有控件,c#,visual-studio-2010,button,commandlink,C#,Visual Studio 2010,Button,Commandlink,我正在动态创建一个列表,并将它们添加到面板中。我创建了一个从面板中删除所有CommandLink控件的方法,它在大部分情况下都能正常工作,除了它似乎只删除其他所有控件。如果我再次调用该方法,它会对其余控件执行相同的操作,只删除其他控件。有人能告诉我哪里出了问题吗?建设性的批评也是受欢迎的 private void MainMenu_Load(object sender, EventArgs e) { #if DEBUG // Generate dummy acti

我正在动态创建一个列表,并将它们添加到面板中。我创建了一个从面板中删除所有
CommandLink
控件的方法,它在大部分情况下都能正常工作,除了它似乎只删除其他所有控件。如果我再次调用该方法,它会对其余控件执行相同的操作,只删除其他控件。有人能告诉我哪里出了问题吗?建设性的批评也是受欢迎的

    private void MainMenu_Load(object sender, EventArgs e)
    {
#if DEBUG
        // Generate dummy actions
        for (int i = 0; i < 20; i++)
        {
            CommandLink cl = AddCommandLink(String.Format("cl{0}",i), String.Format("Command #{0}", i), "The quick brown fox jumps over the lazy dog.", true);
            cl.Click += new EventHandler(CommandLinks_Click);
        }
#endif
    }

    private void CommandLinks_Click(object sender, EventArgs e)
    {
        ClearCommandLinks();
    }

    private CommandLink AddCommandLink( string name, string text, string note = "", bool shield = false )
    {
        int top = 0;
        foreach (Control c in splitMain.Panel2.Controls)
            if (c.GetType() == typeof(CommandLink))
                top = Math.Max(top, ((CommandLink)c).Bottom);
        CommandLink cl = new CommandLink();
        cl.Name = name;
        cl.Location = new Point(10, top + 10);
        cl.Width = splitMain.Panel2.ClientSize.Width - SystemInformation.VerticalScrollBarWidth - 20;
        cl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
        cl.Text = text;
        if (!String.IsNullOrEmpty(note))
            cl.Note = note;
        if (shield)
            cl.Shield = true;
        splitMain.Panel2.Controls.Add(cl);
        return cl;
    }

    private void ClearCommandLinks()
    {
        foreach (Control c in splitMain.Panel2.Controls)
        {
            if (c.GetType() == typeof(CommandLink))
            {
                CommandLink cl = (CommandLink)c;
                splitMain.Panel2.Controls.Remove(cl);
            }
        }
    }
private void主菜单\u加载(对象发送方,事件参数e)
{
#如果调试
//生成虚拟动作
对于(int i=0;i<20;i++)
{
CommandLink cl=AddCommandLink(String.Format(“cl{0}”,i),String.Format(“Command{0}”,i),“敏捷的棕色狐狸跳过了懒狗。”,true);
cl.Click+=新事件处理程序(CommandLinks\u Click);
}
#恩迪夫
}
私有void命令链接\u单击(对象发送方,事件参数e)
{
ClearCommandLinks();
}
private CommandLink AddCommandLink(字符串名称,字符串文本,字符串注释=”,bool shield=false)
{
int-top=0;
foreach(splitMain.Panel2.控件中的控件c)
if(c.GetType()==typeof(CommandLink))
top=Math.Max(top,((CommandLink)c).Bottom);
CommandLink cl=新CommandLink();
cl.名称=名称;
cl.位置=新点(10,顶部+10);
cl.Width=splitMain.Panel2.ClientSize.Width-SystemInformation.VerticalScrollBarWidth-20;
cl.Anchor=主持人风格。左|主持人风格。上|主持人风格。右;
cl.文本=文本;
如果(!String.IsNullOrEmpty(注意))
cl.注释=注释;
如果(屏蔽)
cl.屏蔽=真实;
splitMain.Panel2.Controls.Add(cl);
返回cl;
}
私有void ClearCommandLinks()
{
foreach(splitMain.Panel2.控件中的控件c)
{
if(c.GetType()==typeof(CommandLink))
{
CommandLink cl=(CommandLink)c;
splitMain.面板2.控制装置。拆除(cl);
}
}
}

我认为在迭代时更改集合是个问题

尝试一个for循环:

for (int index = 0; index < splitMain.Panel2.Controls.length...
for(int index=0;index
我认为在迭代时更改集合是个问题

尝试一个for循环:

for (int index = 0; index < splitMain.Panel2.Controls.length...
for(int index=0;index
我认为在迭代时更改集合是个问题

尝试一个for循环:

for (int index = 0; index < splitMain.Panel2.Controls.length...
for(int index=0;index
我认为在迭代时更改集合是个问题

尝试一个for循环:

for (int index = 0; index < splitMain.Panel2.Controls.length...
for(int index=0;index
尝试不要从正在遍历的列表中删除

这样做:

List<Control> removeList = new List<Control>();
foreach (Control c in splitMain.Panel2.Controls)
{
   if (c.GetType() == typeof(CommandLink))
   {
      removeList.Add( c);
   }
}

foreach(Control c in removeList )
{
   splitMain.Panel2.Controls.Remove(c);
}
List removeList=new List();
foreach(splitMain.Panel2.控件中的控件c)
{
if(c.GetType()==typeof(CommandLink))
{
删除列表。添加(c);
}
}
foreach(removeList中的控件c)
{
splitMain.面板2.控制装置。拆除(c);
}

尝试不要从正在遍历的列表中删除

这样做:

List<Control> removeList = new List<Control>();
foreach (Control c in splitMain.Panel2.Controls)
{
   if (c.GetType() == typeof(CommandLink))
   {
      removeList.Add( c);
   }
}

foreach(Control c in removeList )
{
   splitMain.Panel2.Controls.Remove(c);
}
List removeList=new List();
foreach(splitMain.Panel2.控件中的控件c)
{
if(c.GetType()==typeof(CommandLink))
{
删除列表。添加(c);
}
}
foreach(removeList中的控件c)
{
splitMain.面板2.控制装置。拆除(c);
}

尝试不要从正在遍历的列表中删除

这样做:

List<Control> removeList = new List<Control>();
foreach (Control c in splitMain.Panel2.Controls)
{
   if (c.GetType() == typeof(CommandLink))
   {
      removeList.Add( c);
   }
}

foreach(Control c in removeList )
{
   splitMain.Panel2.Controls.Remove(c);
}
List removeList=new List();
foreach(splitMain.Panel2.控件中的控件c)
{
if(c.GetType()==typeof(CommandLink))
{
删除列表。添加(c);
}
}
foreach(removeList中的控件c)
{
splitMain.面板2.控制装置。拆除(c);
}

尝试不要从正在遍历的列表中删除

这样做:

List<Control> removeList = new List<Control>();
foreach (Control c in splitMain.Panel2.Controls)
{
   if (c.GetType() == typeof(CommandLink))
   {
      removeList.Add( c);
   }
}

foreach(Control c in removeList )
{
   splitMain.Panel2.Controls.Remove(c);
}
List removeList=new List();
foreach(splitMain.Panel2.控件中的控件c)
{
if(c.GetType()==typeof(CommandLink))
{
删除列表。添加(c);
}
}
foreach(removeList中的控件c)
{
splitMain.面板2.控制装置。拆除(c);
}

这似乎会导致相同的行为。@druciferre:Don
++index
当您删除时,如果要执行for循环,请按相反的顺序执行。这似乎会导致相同的行为。@druciferre:Don
++index
当您删除时,如果要执行for循环,请按相反的顺序执行。这似乎会导致相同的行为行为。@druciferre:Don
++index
当您删除时,如果要执行for循环,请按相反的顺序执行。这似乎会导致相同的行为。@druciferre:Don
++index
当您删除时,如果要执行for循环,请按相反的顺序执行。您正在违反
foreach
的基本规则——您应该这样做永远不要试图更改正在枚举的集合。有时会出现奇怪的行为,有时会出现异常,很少“正常工作”您正在违反
foreach
的一条基本规则--您不应该尝试更改正在枚举的集合。有时您会出现异常行为,有时会出现异常,但很少“正常工作”您正在违反
foreach
的一条基本规则--您不应该尝试更改正在枚举的集合。有时您会出现异常行为,有时会出现异常,但很少“正常工作”您正在违反
foreach
的一条基本规则--您不应该尝试更改正在枚举的集合。有时您会出现异常行为,有时会出现异常,但很少“正常工作”