richard
richard

Reputation: 12498

Removing control from panel doesn't remove it from the Form?

I have a routine where I loop recursively through all the controls on a form and process some code on some of them.

I add and remove controls through the use of the screen depending on selections the user makes.

I found that panel.Controls.Remove(control1) didn't actually remove it from the form. When I would run the routine that loops recursively through the controls on the form, the control I thought I had remove was still being found.

It didn't "disappear" until I did:

panel.Controls.Remove(control1);
this.Controls.Remove(control1)

Is this expected? Can someone explain this to me, and or point me to somewhere that explains control behavior in Windows Forms.

Thanks!

Upvotes: 1

Views: 3568

Answers (1)

Hans Passant
Hans Passant

Reputation: 941217

Clearly the control has the form as its Parent, not the panel. These kind of accidents tend to happen easily with the designer. You can use View + Other Windows + Document Layout to get a good view of the child-parent relationships. You can use drag+drop in this list to fix.

Upvotes: 3

Related Questions