Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between setting control Parent property and using Controls.Add()?

more specifically, are these statements

ownerControl.GroupBox1.Controls.Remove(childControl);
ownerControl.Controls.Add(childControl);

an equivalent to

childControl.Parent = ownerControl;
like image 394
jonny Avatar asked Jun 07 '09 09:06

jonny


1 Answers

Looking in reflector, it looks like Parent just calls Add (when the new parent is non-null). The Controls.Add deals with taking it away from the old parent. So actually, the following are functionally equivalent (when ownerControl is not null):

ownerControl.Controls.Add(childControl); // note no Remove etc

and:

childControl.Parent = ownerControl;

Counter-intuitive, but a quick test shows that it works.

like image 97
Marc Gravell Avatar answered Oct 16 '22 18:10

Marc Gravell