[英]Adding / removing controls with same reference name
当用户单击按钮时,我一个接一个地在面板上添加控件。 控件在面板上一个接一个显示。 当面板已满时(例如十个),添加的下一个控件必须替换添加的第一个控件,但是首先我需要删除第一个控件。...问题是正在创建的所有控件都具有相同的引用例如“ pic”,现在当我使用panel1.Controls.Remove(pic)时,它没有指定需要删除第一个位置的控件!
int index = 0;
if (util.GetSize() != 10)
{
ms.Controls.Add(musNote);
}
else
{
ms.Controls.Add(musNote); //this to replace first musNote added... but before I need to remove the musNote that in that position already (problem all of the are musNote!!
index++;
if (index == 10)
{
index = 0;
}
}
musNote.ShowNote();
以上不是全部代码,但足以解决问题..如果有任何信息。 需要,请问我。 提前感谢。
如果只想删除第一个孩子,请使用
RemoveAt方法
myPanel.Controls.RemoveAt(0);
不要使用上面的代码,它会在Handle
泄漏,使用Dispose作为@Hans在注释中建议
myPanel.Controls[0].Dispose();//This takes care of destroying the window
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.