LayoutEngine::Layout Method (Object^, LayoutEventArgs^)
Requests that the layout engine perform a layout operation.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Parameters
- container
-
Type:
System::Object^
The container on which the layout engine will operate.
- layoutEventArgs
-
Type:
System.Windows.Forms::LayoutEventArgs^
An event argument from a Layout event.
Return Value
Type: System::Booleantrue if layout should be performed again by the parent of container; otherwise, false.
| Exception | Condition |
|---|---|
| NotSupportedException | 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: 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; }
Available since 2.0