jonny
jonny

Reputation: 736

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;

Upvotes: 6

Views: 3082

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063569

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.

Upvotes: 12

Related Questions