Layout Manager Settings in Windows Forms LayoutManagers

The settings that are common to all the Layout Manager’s have been discussed in this section.

Behavior settings

The behavior settings that are common to all the Layout Managers are discussed below.

AutoLayout

The Layout Manager, by default, listens to the Container’sLayout events and performs the layout automatically.

LayoutManager property Description
AutoLayout Indicates whether the Layout Manager should layout automatically on Layout event.
this.borderLayout1.AutoLayout = true;
Me.borderLayout1.AutoLayout = True

NOTE

The above process can be prevented by setting the AutoLayout property to ‘False’ and invoking the layout explicitly through a call to the LayoutContainer() method.

ContainerControl

The Container control to be laid out by the Layout Manager can be specified using the below given property.

LayoutManager property Description
ContainerControl Specifies the Container control that the Layout Manager will layout.
this.borderLayout1.ContainerControl = this;
Me.borderLayout1.ContainerControl = Me

Custom layout bounds

The Layout Manager will, by default, layout the Child components within the Container control’s client rectangle. However, you can specify any custom layout bounds using the property given below.

LayoutManager property Description
CustomLayoutBounds Specifies the custom layout bounds, if any, to be used for layout calculation instead of the Container control's client rectangle.
this.borderLayout1.CustomLayoutBounds = new System.Drawing.Rectangle(0, 0, 0, 0);
Me.borderLayout1.CustomLayoutBounds = New System.Drawing.Rectangle(0, 0, 0, 0)

NOTE

When you specify the custom layout bounds and the Container is resizable, you should also set the AutoLayout property to ‘False’ and set a new custom layout Note: The layout is done within the Container’s client rectangle, even if the Container has a scrollable display rectangle.

Margin settings

The margin settings that are common to all the Layout Managers are discussed below.

The layout bounds will also be adjusted to include some margin space along the borders according to the values specified in the properties given below. The default values of these properties are set to ‘Zero’.

LayoutManager properties Description
TopMargin Gets/sets the top margin between the client rectangle and the layout rectangle.
HortNearMargin Gets/sets the left margin between the client rectangle and the layout rectangle.
HortFarMargin Gets/sets the right margin between the client rectangle and the layout rectangle.
BottomMargin Gets/sets the bottom margin between the client rectangle and the layout rectangle.
this.borderLayout1.TopMargin = 20;

this.borderLayout1.HorzFarMargin = 20;

this.borderLayout1.HorzNearMargin = 20;

this.borderLayout1.BottomMargin = 20;
Me.borderLayout1.TopMargin = 20

Me.borderLayout1.HorzFarMargin = 20

Me.borderLayout1.HorzNearMargin = 20

Me.borderLayout1.BottomMargin = 20

Child controls arranged with margin in layout manager

See Also

Configuring BorderLayout, Configuring FlowLayout, Configuring GridLayout.

Child control settings

The Child control settings for the Layout Managers are given below.

Size

Preferred size

The Layout Managers usually layout the components based on their preferred sizes. But a .NET control does not provide information regarding it’s preferred size. To overcome this, a PreferredSize extended property is provided for each Child control at design time.

In code, you can perform the same using the methods given below.

Methods Description
SetPreferredSize Associates a preferred size with the specified control.
GetPreferredSize Retrieves the preferred size associated with the specified control.
this.cardLayout1.SetPreferredSize(this.button1, new System.Drawing.Size(75, 92));
Me.cardLayout1.SetPreferredSize(Me.button1, New System.Drawing.Size(75, 92))

Minimum size

You can similarly associate a minimum size for a Child component through the MinimumSize extended property. However, some Layout Managers ignore this setting. Refer to the individual Layout Managers for more information on how the size plays an important part in the layout logic.

In code, you can perform the same using the methods given below.

Methods Description
SetMinimumSize Associates a minimum size with the specified control.
GetMinimumSize Retrieves the minimum size associated with the specified control.
this.cardLayout1.SetMinimumSize(this.button1, new System.Drawing.Size(75, 92));
Me.cardLayout1.SetMinimumSize(Me.button1, New System.Drawing.Size(75, 92))

You can also dynamically provide preferred and minimum size information for a Child component at run time. The manner in which a Layout Manager determines the preferred size for a Child control is illustrated below.

  • The layout manager checks if the Child control/component implements the IProvideLayoutInformation interface. If so, it calls into that interface to retrieve the preferred size.
  • If the above step fails, the Layout Manager fires the ProvideLayoutInformation event, requesting the size information required. If the event is handled and the information provided, that size will be used.
  • If the above step fails, the Layout Manager checks if a preferred size was provided statically during design time using the extended PreferredSize property or in code using the SetPreferredSize() method. If so, that size is used. If not, the current size of the Child control is made the preferred size and that size will be used.

Child control arrangement architecture in layout manager

NOTE

The same steps are used to determine the minimum size, if required, for a Child control.

The above properties are available as the extended properties for the Child controls of CardLayout, FlowLayout and GridBagLayout only.

See Also

BorderLayout – Configuring Child Control, CardLayout - Configuring Child Controls, FlowLayout - Configuring Child Controls, GridLayout - Configuring Child Controls, GridBagLayout - Configuring Child Controls