LayoutEngine.Layout(Object, LayoutEventArgs) Metodo

Definizione

Richiede che il motore di layout esegua un'operazione di layout.

public:
 virtual bool Layout(System::Object ^ container, System::Windows::Forms::LayoutEventArgs ^ layoutEventArgs);
public virtual bool Layout (object container, System.Windows.Forms.LayoutEventArgs layoutEventArgs);
abstract member Layout : obj * System.Windows.Forms.LayoutEventArgs -> bool
override this.Layout : obj * System.Windows.Forms.LayoutEventArgs -> bool
Public Overridable Function Layout (container As Object, layoutEventArgs As LayoutEventArgs) As Boolean

Parametri

container
Object

Contenitore sul quale agirà il motore di layout.

layoutEventArgs
LayoutEventArgs

Argomento di evento restituito da un evento Layout.

Restituisce

true se il layout deve essere nuovamente eseguito dal padre dell'oggetto container; in caso contrario, false.

Eccezioni

L'oggetto container non è un tipo sul quale l'oggetto LayoutEngine può eseguire il layout.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso del metodo per implementare il comportamento del Layout layout personalizzato. Questo esempio di codice fa parte di un esempio più ampio fornito per la LayoutEngine classe .

public:
    virtual bool Layout(Object^ container,
        LayoutEventArgs^ layoutEventArgs) override
    {
        Control^ parent = nullptr;
        try
        {
            parent = (Control ^) container;
        }
        catch (InvalidCastException^ ex)
        {
            throw gcnew ArgumentException(
                "The parameter 'container' must be a control", "container", ex);
        }
        // Use DisplayRectangle so that parent.Padding is honored.
        Rectangle parentDisplayRectangle = parent->DisplayRectangle;
        Point nextControlLocation = parentDisplayRectangle.Location;

        for each (Control^ currentControl in parent->Controls)
        {
            // Only apply layout to visible controls.
            if (!currentControl->Visible)
            {
                continue;
            }

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

            // Set the location of the control.
            currentControl->Location = nextControlLocation;

            // Set the autosized controls to their
            // autosized heights.
            if (currentControl->AutoSize)
            {
                currentControl->Size = currentControl->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 += currentControl->Height +
                currentControl->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;
    }
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;
}
Public Overrides Function Layout( _
ByVal container As Object, _
ByVal layoutEventArgs As LayoutEventArgs) As Boolean

    Dim parent As Control = container

    ' Use DisplayRectangle so that parent.Padding is honored.
    Dim parentDisplayRectangle As Rectangle = parent.DisplayRectangle
    Dim nextControlLocation As Point = parentDisplayRectangle.Location

    Dim c As Control
    For Each c In parent.Controls

        ' Only apply layout to visible controls.
        If c.Visible <> True Then
            Continue For
        End If

        ' 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 Then
            c.Size = c.GetPreferredSize(parentDisplayRectangle.Size)
        End If

        ' 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
    Next c

    ' 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

End Function

Commenti

Questo metodo viene chiamato quando il motore di layout deve eseguire un'operazione di layout sul container parametro . È possibile controllare il valore delle AffectedPropertyproprietà , AffectedComponente AffectedControl su layoutEventArgs per decidere se è necessaria un'operazione di layout.

Note per gli eredi

Eseguire l'override del Layout(Object, LayoutEventArgs) metodo per fornire il comportamento del layout personalizzato.

Quando si dispone il contenuto del container parametro, assicurarsi di controllare la Visible proprietà di ogni controllo figlio.

Restituisce true se la logica del motore di layout determina che il layout deve essere eseguito nuovamente dall'elemento padre del contenitore. Ciò può verificarsi, ad esempio, quando il motore di layout ridimensiona i controlli figlio e determina che il contenitore deve essere aumentato di dimensioni per supportare il nuovo layout.

Si applica a

Vedi anche