Measure and Arrange - ahatornn/clforms GitHub Wiki

To determine where the element will be located and what dimensions it has, there is a kernel mechanism that monitors the property of the IsMeasureValid component. As soon as the kernel detects at least one element with the value of this property false, the mechanism of redefining positioning and resize will start. The mechanism operates in two stages.

Measure

A method that, given availableSize, determines the desired sizes and sets them to DesiredSize. The available space that a parent element can allocate a child element.

// Updates the DesiredSize of a Control". Parent elements call this method from their own 
// Measure(Size) implementations to form a recursive layout update
public virtual void Measure(Size availableSize)

The essence of the method is to call Measure on all children in order to determine their desired size. After that, define your own size so that all child elements fit inside. To determine the area accessible to all child elements, it is also necessary to take into account the Margin and Padding properties of the current element. Also, do not forget about the AutoSize property, which in the case of the value false, in most cases sets the DesiredSize equal value of the parameter availableSize.
Calling Measure of the base class Control sets the value for DesiredSize, and also IsMeasureValid is set to true. However, the IsVisualValid property is set to false, because Now it will be necessary to redraw the component.

Arrange

The method tells the control its location (x, y) and the size of the rectangle. Everything that goes beyond these limits must be cut off.

// Positions child elements and determines a size for a Control. Parent elements call
// this method from their Arrange(Rect) implementation to form a recursive layout update
public virtual void Arrange(Rect finalRect, bool reduceMargin = true)

Parameter finalRect defines the final size that the parent computes for the child element provided as a Rect instance. Parameter reduceMargin indicates if should reduce margin from final rect value.

If you have a property, changing the value of which can affect the position or size of the control, do not forget to set IsMeasureValid to false.

InvalidateMeasure();