Using a LayoutControl in Inherited Forms
- 2 minutes to read
Microsoft Visual Studio provides the Visual Inheritance (VI) feature that allows a developer to design a base form (user control) and then inherit it at design time; extending it by adding new controls and customizing inherited ones. However, VI has some limitations on using composite controls (including the LayoutControl) on inherited forms.
The Layout Control does not support design-time modifications in inherited forms when using Windows Forms Visual Inheritance (VI). Thus, if you are utilizing the Layout Control in inherited forms, it is necessary to consider the following.
- At design time, do not change the Layout Control layout in a descendant form (add, move or remove layout items).
- At design time, do not change the control’s properties, and its items in the Properties window, in a descendant form.
- At design time, you should customize the layout of each Layout Control in the form in which it was created. For instance, if a LayoutControl is created in a base form, its layout must only be customized at design time in this form, rather than in derived forms.
- You can freely customize the Layout Control layout in code, both in base and inherited forms.
To avoid accidental modification of the LayoutControl and its items within inherited forms, you can make these objects private within the base form.
Assume that you need to create a base form with a LayoutControl used to arrange all controls, and wish to use VI to clone the layout within inherited forms. The layout can be divided into three rectangular regions, as illustrated in the image below.
The A and B regions will be reproduced without modification in all inherited forms, while region C is unique for each inherited form. To meet the VI rules, instead of creating a base form with a single LayoutControl, you should use three layout controls, as outlined below.
- Create a base form with two LayoutControls within it. These should represent the A and B regions.
- Customize these LayoutControls as required, within the base form.
- Create an inherited form using VI and add a new LayoutControl to the form, to represent region C.
- Customize this LayoutControl within the inherited form.
With this solution, there is no need to customize the layout of inherited LayoutControls, which allows you to avoid potential problems with VI.