LayoutEngine.Layout(Object, LayoutEventArgs) Method

Definition

Requests that the layout engine perform a layout operation.

C#
public virtual bool Layout(object container, System.Windows.Forms.LayoutEventArgs layoutEventArgs);

Parameters

container
Object

The container on which the layout engine will operate.

layoutEventArgs
LayoutEventArgs

An event argument from a Layout event.

Returns

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

Exceptions

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

Examples

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.

C#
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)
        {
            continue;
        }

        // 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;
}

Remarks

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(Object, LayoutEventArgs) 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.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also