How to dynamically remove a UserControl in c # .net from the form?

2

I have a UserControl that is dynamically added to a FlowLayoutPanel . In that same UserControl I have a button to remove itself if the user wants, obviously at runtime. I need to delete it from Form . I tried this but nothing happened because the "Children" does not recognize me:

((FlowLayoutPanel)this.Parent).Children.Remove(this);
    
asked by JaviYeri 14.06.2018 в 06:47
source

2 answers

0

The best answer and it was useful for me was given in StackOverflow in English. As it is not duplicated in Spanish I leave it and I do not eliminate the question. Thanks to everyone!

Click here to go to the answer to the same question in this thread

    
answered by 17.06.2018 / 05:23
source
2

Hi, I'll leave you an example working. In the click event of the button that you have in your UserControl we have:

 private void button1_Click(object sender, EventArgs e)
    {
        this.Parent.Controls.Remove(this);
    }

EDITED

In the original Form you should enter this:

 this.userControl11.Parent = this;

I hope it helps you.

    
answered by 14.06.2018 в 08:05