Containment
All useful forms contain some
controls. There is more to this containment relationship than meets
the eye, and if you are familiar with the old Win32 parent/child
relationship, you will find that things do not work in quite the same
way. We will look at the control nesting facilities supplied by both
the Control
class and the
ContainerControl
class, paying particular
attention to the implications of containment for focus and validation
events.
Parents and Owners
Controls rarely exist in complete isolation—top-level windows usually contain some controls, and all non-top-level controls are associated with a window. In fact, Windows Forms defines two kinds of relationships between controls. There is the parent/child relationship, which manages containment of controls within a single window. There is also a looser association that can exist between top-level windows, which is represented by the owner/owned relationship.
Parent and child
A child window is one that is completely contained by its parent. For example, any controls that you place on a form are children of that form. A child’s position is specified relative to its parent, and the child is clipped to the parent’s bounds—i.e., only those parts of the child completely inside the parent are visible. Forms can be children too: document windows in an MDI application are children of the main MDI frame.
A control’s parent is accessible through its
Parent
property (of type
Control
). If you examine this property on ...
Get .NET Windows Forms in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.