Registriert seit: 26. Jun 2008
27 Beiträge
|
Re: Klasse soll Anwendung nur bei best. Änderung benachricht
27. Jun 2008, 09:48
Oh sorry wollte nur den Thread nicht wieder in die Länge ziehen.
Unit1:
Delphi-Quellcode:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure DoChange;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
uses TestClass;
{$R *.dfm}
var
myClass : TMyclass;
//Programmstart
procedure TForm1.FormCreate(Sender: TObject);
begin
myClass := TMyclass.Create;
myClass.OnChange_Func := DoChange;
end;
procedure TForm1.DoChange;
begin
ShowMessage(' Status geändert');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
myClass.Timer.enabled := NOT myClass.Timer.enabled;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
myClass.Status := NOT myClass.Status
end;
end.
Klasse:
Delphi-Quellcode:
unit Testclass;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Forms, Dialogs,
StdCtrls, WinSock, ActiveX, ComObj, ExtCtrls;
type
TOnChange_Proc = procedure() of object;
TMyClass = class(TObject)
private
FTimer : TTimer;
FStatus : Boolean;
FOnChange : TOnChange_Proc;
procedure OnChange;
function getAvailable: Boolean;
procedure testTimer(Sender : TObject);
procedure setStatus(value: Boolean);
public
constructor Create;
destructor destroy; override;
property OnChange_Func : TOnChange_Proc read FOnChange write FOnChange;
property Status : Boolean read FStatus write setStatus;
property Timer : TTimer read FTimer write FTimer;
end;
implementation
constructor TMyClass.Create;
begin
inherited;
FTimer := TTimer.Create( nil);
FTimer.enabled := false;
FTimer.Interval := 1000;
FTimer.OnTimer := testTimer;
FOnChange := nil;
end;
destructor TMyClass.Destroy;
begin
FTimer.Free;
inherited;
end;
procedure TMyClass.OnChange;
begin
if Assigned(FOnChange) then
FOnChange;
end;
procedure TMyClass.setStatus(value: Boolean);
begin
FStatus := value;
if Assigned(FOnChange) then
FOnChange;
end;
procedure TMyClass.testTimer(Sender:TObject);
begin
GetAvailable;
onchange;
end;
function TMyClass.GetAvailable: Boolean;
begin
if FStatus = true then
result := true
else
result := false
end;
end.
|
|
Zitat
|