This documentation is archived and is not being maintained.

LayoutEngine.Layout Method

Note: This method is new in the .NET Framework version 2.0.

Requests that the layout engine perform a layout operation.

Namespace: System.Windows.Forms.Layout
Assembly: System.Windows.Forms (in

public virtual bool Layout (
	Object container,
	LayoutEventArgs layoutEventArgs
public boolean Layout (
	Object container, 
	LayoutEventArgs layoutEventArgs
public function Layout (
	container : Object, 
	layoutEventArgs : LayoutEventArgs
) : boolean



The container on which the layout engine will operate.


An event argument from a Layout event.

Return Value

true if layout should be performed again by the parent of container; otherwise, false.

Exception typeCondition


container is not a type on which LayoutEngine can perform layout.

This method is called when the layout engine is to perform a layout operation on the container parameter. You can check the value of the AffectedProperty, AffectedComponent, and AffectedControl properties on layoutEventArgs to decide if a layout operation is necessary.

Notes to Inheritors Override the Layout method to provide your custom layout behavior. When laying out the contents of the container parameter, be sure to check the Visible property of each child control. Return true if your layout engine logic determines that layout should be performed again by the parent of the container. This might occur, for example, when the layout engine resizes child controls and determines that the container must be increased in size to accommodate the new layout.

The following code example demonstrates the use of the Layout method to implement custom layout behavior. This code example is part of a larger example provided for the LayoutEngine class.

public override bool Layout(
    object container,
    LayoutEventArgs layoutEventArgs)
    Control parent = container as Control;

    // Use DisplayRectangle so that parent.Padding is honored.
    Rectangle parentDisplayRectangle = parent.DisplayRectangle;
    Point nextControlLocation = parentDisplayRectangle.Location;

    foreach (Control c in parent.Controls)
        // Only apply layout to visible controls.
        if (!c.Visible)

        // Respect the margin of the control:
        // shift over the left and the top.
        nextControlLocation.Offset(c.Margin.Left, c.Margin.Top);

        // Set the location of the control.
        c.Location = nextControlLocation;

        // Set the autosized controls to their 
        // autosized heights.
        if (c.AutoSize)
            c.Size = c.GetPreferredSize(parentDisplayRectangle.Size);

        // Move X back to the display rectangle origin.
        nextControlLocation.X = parentDisplayRectangle.X;

        // Increment Y by the height of the control 
        // and the bottom margin.
        nextControlLocation.Y += c.Height + c.Margin.Bottom;

    // Optional: Return whether or not the container's 
    // parent should perform layout as a result of this 
    // layout. Some layout engines return the value of 
    // the container's AutoSize property.

    return false;

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0