IComponent.Components

Syntax

Components(Index: Integer): IComponent;

Parameters

Index. Index of the component requiring a link.

Description

The Components property returns the child property, which index is passed by the input parameter.

Example

Executing the example requires a form and any components on it. The OnCreate event handler is created for the form.

Class TESTForm: Form
    //...
    //List of the form components
    //...

    Sub TESTFormOnCreate(Sender: Object; Args: IEventArgs);
    Begin
        //Prepare components for form working
        PrepareComponents(Self);
    End Sub TESTFormOnCreate;

    Sub PrepareComponents(Component: IComponent);
    Var
        i: Integer;
        ChildComponent: IComponent;
    Begin
        For i := 0 To Component.ComponentCount - 1 Do
            ChildComponent := Component.Components(i);
            //Write component index relative to its parent component to Tag
            ChildComponent.Tag := i;
            //If the component is the button, then write it to the BtnOnClick event
            If ChildComponent Is Button Then
                (ChildComponent As Button).OnClick := BtnOnClick;
            End If;
            ChildComponent.Data := GuidGenerator.Generate;
            //If the component is the container for other components, then prepare them recursively
            If ChildComponent.ComponentCount > 0 Then
                PrepareComponents(ChildComponent);
            End If;
        End For;
    End Sub PrepareComponents;

    Sub BtnOnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        Comp: IComponent;
    Begin
        Comp := Sender As IComponent;
        Debug.WriteLine(Comp.Name);
        Debug.WriteLine(Comp.Data);
    End Sub BtnOnClick;

End Class TESTForm;

On starting the form, recursive iteration of all components and setting of basic properties are executed: the sequence number of the component relative to its parent component is stored in the Tag property; unique global identifier is generated for each component in the Data property. All buttons will also be subscribed to the BtnOnClick general event.

See also:

Forms Assembly Interfaces