C# 删除面板中所有动态创建的面板

C# 删除面板中所有动态创建的面板,c#,panel,C#,Panel,我有一个联系人管理应用程序(你帮了我很多:D),在我的主窗体上有一个名为panel_contact的面板。内部有面板,其中包含显示的触点: 但我的问题是: 创建联系人时,将重新加载“联系人”面板以添加我刚刚创建的联系人。例如,在下面的图片中,我添加了Jackie,你可以看到第一个联系人已经复制了自己,这不是我想要的 那么,如何删除联系人面板中的所有面板? 我已经试过了: panel_contact.Controls.Clear(); //Don't work panel1.Invalidat

我有一个联系人管理应用程序(你帮了我很多:D),在我的主窗体上有一个名为panel_contact的面板。内部有面板,其中包含显示的触点:

但我的问题是: 创建联系人时,将重新加载“联系人”面板以添加我刚刚创建的联系人。例如,在下面的图片中,我添加了Jackie,你可以看到第一个联系人已经复制了自己,这不是我想要的

那么,如何删除联系人面板中的所有面板? 我已经试过了:

panel_contact.Controls.Clear(); //Don't work
panel1.Invalidate(); //Don't work
有人有办法吗? 多谢各位

编辑: 完整代码:

namespace Contact
{
    public partial class Contact : Form
    {

        List<Personne> contacts = new List<Personne>(); //Contient tous les contacts

        public Contact()
        {
            this.Icon = AppResources.icon;
            InitializeComponent();
        }


        private void Picture_add_Click(object sender, EventArgs e)
        {
            //Ouvre la fenêtre de création de contact
            //Add.cs
            Add ajouterWindow = new Add();
            ajouterWindow.ShowDialog();

            this.Contact_Load(this, null);
        }

        private void Contact_Load(object sender, EventArgs e)
        {
            //Création des class contact depuis le fichier txt

            string path = System.IO.Path.GetFullPath(@"contact.txt");

            try
            {
                List<string> contactsBrut = File.ReadAllLines(path).ToList();

                string[] inforamtions = new string[15];
                string[] notes = new string[8];

                for (int i = 0; i < contactsBrut.Count; i++)
                {
                    inforamtions = contactsBrut[i].Split('#');

                    for (int z = 0; z < inforamtions.Length; z++) //remplace les valeurs incorrect par null
                    {
                        if (String.IsNullOrWhiteSpace(inforamtions[z]) || String.IsNullOrEmpty(inforamtions[z]))
                        {
                            inforamtions[z] = null;
                        }
                    }

                    int age = Convert.ToInt32(inforamtions[2]);
                    bool isMan = false;

                    if (inforamtions[4] == "True") {
                        isMan = true;
                    }
                    else {
                        isMan = false;
                    }

                    contacts.Add(new Personne(inforamtions[0], inforamtions[1], age, inforamtions[3], isMan,
                       inforamtions[5], inforamtions[6], inforamtions[7], inforamtions[8], inforamtions[9], inforamtions[10], inforamtions[11],
                       inforamtions[12], inforamtions[13], inforamtions[14], inforamtions[15]));
                }

            }
            catch
            {
                MessageBox.Show("Erreur avec les données de l'application"); 
            }

            //Afficher les contacts
            if(contacts.Count == 0)
            {
                label_aucunContact.Show();
            }
            else
            {
                label_aucunContact.Hide();

                ClearPanel();
                afficherContact();

            }
            //Continuation du load
        }

        private void afficherContact()
        {
            int x = 4;
            int y = 4;
            for (int i = 0; i < contacts.Count; i++)
            {
                var control = new PersonControl(contacts[i]);
                control.Location = new Point(x, y);
                panel_contact.Controls.Add(control);
                y += control.Height + 2;
            }
        }

        private void Picture_add_MouseHover(object sender, EventArgs e)
        {
            ToolTip tt = new ToolTip();
            tt.SetToolTip(this.picture_add, "Ajouter un contact");
        }

        private void ClearPanel()
        {
            foreach (var control in panel_contact.Controls.OfType<PersonControl>().ToList())
                panel_contact.Controls.Remove(control);
        }

    }
}
名称空间联系人
{
公共部分类联系人:表单
{
List contacts=new List();//Contient tous les contacts
公众联络()
{
this.Icon=AppResources.Icon;
初始化组件();
}
私有无效图片\u添加\u单击(对象发送者,事件参数e)
{
//我们的联系方式不同
//Add.cs
Add ajouterWindow=new Add();
ajouterWindow.ShowDialog();
this.Contact_Load(this,null);
}
私有无效联系人加载(对象发送方,事件参数e)
{
//类联系人地址:le fichier txt
字符串路径=System.IO.path.GetFullPath(@“contact.txt”);
尝试
{
List contactsBrut=File.ReadAllLines(path.ToList();
字符串[]信息=新字符串[15];
字符串[]注释=新字符串[8];
对于(int i=0;i
以下是该项目的链接:

假设我们继续使用用户控制解决方案,您可以编写:

using System.Linq;

private void ClearPersonControls(Panel panel)
{
  foreach ( var control in panel.Controls.OfType<PersonControl>().ToList() )
    panel.Controls.Remove(control);
}
其中
createPersonControl
是一种包含上一个问题的代码的方法,用于使用一些
PersonControl

当然,如果行数不多,您可以将所有代码同时放入
UpdatePanel
方法中,而无需创建
Clear
Create
。做你想做的事

如果需要,可以添加刷新,但在看到代码后,不需要:

panel.Refresh();

如何首先添加重复条目?你必须避免这种情况。确切地说,这就是我试图解决的问题:(你需要迭代已经添加的
Person
对象,如果不存在,则添加一个新的
Person
。嗯,它不起作用,是的,是关于这个项目的。也许我放错地方了?我在动态添加userControl的for循环之前测量它:'(这是相同的代码,但有一个奇怪的,你能在你的项目文件夹中发布一些代码或zip吗?没关系!没关系!我更改了代码的位置现在一切正常非常感谢!好的,问题不在于ClearPanel和CreatePanel:问题是当你添加一个新联系人时,你会重新加载列表,这样重复的联系人就可以了如果需要,添加
contacts.Clear()
Contact\u Load
的开头。实际上,您也可以在
afficherContact
的开头移动
ClearPanel
。我的答案中的代码是,如果有一个
UpdatePanel
调用
Clear
Create
。但是由于方法很小,您可以同时将代码放在一个方法中时间。更新答案。
panel.Refresh();