Delphi - Klasse mit Ereignis (weiteres Beispiel)
{
Author : http://www.michael-puff.de
Date : 2006-02-16
License : PUBLIC DOMAIN
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure OnFooProgress(Sender: TObject; Current, Max: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TOnProgress = procedure(Sender: TObject; Current, Max: Integer) of object;
TFoobar = class(TObject)
private
FOnProgress: TOnProgress;
public
procedure DoSomething;
property OnProgress: TOnProgress read FOnProgress write FOnProgress;
end;
procedure TFoobar.DoSomething;
const
MAX = 9;
var
i: Integer;
begin
for i := 0 to MAX do
begin
if Assigned(OnProgress) then
FOnProgress(self, i, MAX);
sleep(500);
end;
end;
procedure TForm1.OnFooProgress(Sender: TObject; Current, Max: Integer);
begin
Progressbar1.Max := Max;
Progressbar1.Step := 1;
Progressbar1.Position := Current;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Foobar: TFoobar;
begin
Foobar := TFoobar.Create;
try
Foobar.OnProgress := OnFooProgress;
Foobar.DoSomething;
finally
FreeAndNil(Foobar);
end;
end;
end.