Simple Box

What is the simplest custom component we can build to teach newbies?

For an absolute beginner in Lazarus/FPC, the simplest possible custom component is probably:

A colored square that just paints itself — no special properties, no events — just enough to demonstrate:

  1. Creating a new component unit
  2. Overriding Paint
  3. Registering it in the palette so you can drop it on a form

Example: TSimpleBox

unit SimpleBox;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Controls, Graphics;

type
  TSimpleBox = class(TGraphicControl)
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TSimpleBox]); // Adds it to Lazarus "Samples" tab
end;

constructor TSimpleBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 50;
  Height := 50;
end;

procedure TSimpleBox.Paint;
begin
  Canvas.Brush.Color := clRed;
  Canvas.FillRect(ClientRect);
end;

end.

Why this is good for teaching newbies: