unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TValueDisplay }
TValueDisplay = class
private
FDisplayLabel: TLabel;
public
property DisplayLabel: TLabel read FDisplayLabel;
var
DoUpdate: TNotifyEvent; // called if the value should be updated
constructor Create(aDisplayLabel: TLabel); // Bind this display to a certain Label
end;
TValueDisplays = array of TValueDisplay; // the list of all value displays
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
procedure FormCreate(Sender: TObject); // Create the form and register a bunch of displays
procedure Timer1Timer(Sender: TObject); // a timer changing a value every 1 second and triggering the update of all registered displays
private
FdoDisplayValue: TNotifyEvent;
FTicks: word;
FValueDisplays: TValueDisplays;
procedure SetdoDisplayValue(AValue: TNotifyEvent);
public
function AddValueDisplay(aDisplay: TValueDisplay): TValueDisplay; // register a display
property ValueDisplays: TValueDisplays read FValueDisplays; // the list of registered displays
property doDisplayValue: TNotifyEvent read FdoDisplayValue write SetdoDisplayValue; // updates the display passed in the sender parameter of TNotifyEvent
property Ticks: word read FTicks; // just a changing value
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TValueDisplay }
constructor TValueDisplay.Create(aDisplayLabel: TLabel);
begin
FDisplayLabel := aDisplayLabel;
FDisplayLabel.Caption := '##';
end;
{ TForm1 }
procedure TForm1.Timer1Timer(Sender: TObject);
var
d: TValueDisplay;
begin
Inc(FTicks);
for d in ValueDisplays do
if assigned(d.DoUpdate) then // if d.DoUpdate <> NIL then
d.DoUpdate(d.DisplayLabel);
end;
procedure TForm1.SetdoDisplayValue(AValue: TNotifyEvent);
begin
if FdoDisplayValue = AValue then Exit;
FdoDisplayValue := AValue;
end;
function TForm1.AddValueDisplay(aDisplay: TValueDisplay): TValueDisplay;
begin
SetLength(FValueDisplays, length(FValueDisplays) + 1);
FValueDisplays[length(FValueDisplays) - 1] := aDisplay;
Result := FValueDisplays[length(FValueDisplays) - 1];
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with AddValueDisplay(TValueDisplay.Create(Self.Label1)) do
DoUpdate := doDisplayValue;
with AddValueDisplay(TValueDisplay.Create(Self.Label2)) do
DoUpdate := doDisplayValue;
end;
end.