TheBreeze
asked on
Executing Events in components
Hi, I've just recently launched myself into component-writing. I have written a sample component with the objective of adding a new event, namely OnNewTransaction
which will be triggered whenever the OnTimer event of a timer is triggered. Below are some portions of that class declaration :
type
TRtStkData = class(TComponent)
private
FTimer : TTimer;
FOnNewTransaction : TNotifyEvent;
FClose : TStringList;
procedure FTimerOnTimer(Sender:TObje ct);
procedure SetClose(Value: TStringList);
procedure AddItem;
protected
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Show(index : integer);
procedure Addnew;
published
property OnNewTransaction : TNotifyEvent read FOnNewTransaction write FOnNewTransaction;
property Close : TStringList read FClose write SetClose;
end;
constructor TRtStkData.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FClose:=TStringList.Create ;
FTimer:=TTimer.Create(Self );
FTimer.Interval:=2000;
FTimer.Enabled:=false;
FTimer.OnTimer:=FTimerOnTi mer;
end;
procedure TRtStkData.SetClose(Value: TStringList);
begin
FClose:=Value;
end;
procedure TRtStkData.add;
begin
FClose.Add(IntToStr(random (100)));
end;
procedure TRtStkData.FTimerOnTimer(S ender:TObj ect);
begin
Add;
if Assigned(FOnNewTransaction ) then
FOnNewTransaction(Self);
end;
The class compiled successfully and the new component was installed successfully. I've made a sample calling program into which I've added the new component.
I enabled the timer at one point of the calling program.The OnNewTransaction event appeared in the Object inspector but that event is never triggered.
When I traced the program I found out that the problem is in the method FTimerOnTimer :
the statement Assigned(FOnNewTransaction )is always false.
Can someone help me out?
Below are some portions of the sample calling program:
procedure TForm1.FormCreate(Sender: TObject);
begin
RtStkData1:=TRtStkData.Cre ate(Self);
end;
procedure TForm1.RtStkData1NewTransa ction(Send er: TObject);
begin
Memo1.Lines.Add(RtStkData1 .Close[RtS tkData1.Cl ose.Count- 1]);
end;
which will be triggered whenever the OnTimer event of a timer is triggered. Below are some portions of that class declaration :
type
TRtStkData = class(TComponent)
private
FTimer : TTimer;
FOnNewTransaction : TNotifyEvent;
FClose : TStringList;
procedure FTimerOnTimer(Sender:TObje
procedure SetClose(Value: TStringList);
procedure AddItem;
protected
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
procedure Show(index : integer);
procedure Addnew;
published
property OnNewTransaction : TNotifyEvent read FOnNewTransaction write FOnNewTransaction;
property Close : TStringList read FClose write SetClose;
end;
constructor TRtStkData.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
FClose:=TStringList.Create
FTimer:=TTimer.Create(Self
FTimer.Interval:=2000;
FTimer.Enabled:=false;
FTimer.OnTimer:=FTimerOnTi
end;
procedure TRtStkData.SetClose(Value:
begin
FClose:=Value;
end;
procedure TRtStkData.add;
begin
FClose.Add(IntToStr(random
end;
procedure TRtStkData.FTimerOnTimer(S
begin
Add;
if Assigned(FOnNewTransaction
FOnNewTransaction(Self);
end;
The class compiled successfully and the new component was installed successfully. I've made a sample calling program into which I've added the new component.
I enabled the timer at one point of the calling program.The OnNewTransaction event appeared in the Object inspector but that event is never triggered.
When I traced the program I found out that the problem is in the method FTimerOnTimer :
the statement Assigned(FOnNewTransaction
Can someone help me out?
Below are some portions of the sample calling program:
procedure TForm1.FormCreate(Sender: TObject);
begin
RtStkData1:=TRtStkData.Cre
end;
procedure TForm1.RtStkData1NewTransa
begin
Memo1.Lines.Add(RtStkData1
end;
I can't find in your code FTimer.Enabled := True;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
listening :-)
Hi,
One thing, did you add code to the Event Handler ? I mean did you double click on the OnNewTransaction event in the object inspector and write some code in that event handler ?
The following code :
if Assigned(FOnNewTransaction ) then
FOnNewTransaction(Self);
the assigned will only be true if you have added some code in the event handler of the OnNewTransaction event.
One last note, your component has a memory leak. Why, well it is a good practice in Component Buidling to destroy everything you created yourself. In the constructor you instantiate a TStringList and a TTimer. Since the TTimer has an owner it will be cleaned up automatically when the Owner component gets destroyed. The TStringList on the other hand doesn't have an owner and will not be destroyed automatically, thus it will stay in memory causing a memory leak.
You should override the destructor of your component and in your case destroy the TTimer and TStringList in the destructory of your component. ( possibly you did it, but you didnt include the destructor in your code sample ).
Hoping that this will be of any help to you.
Best regards,
Stefaan
One thing, did you add code to the Event Handler ? I mean did you double click on the OnNewTransaction event in the object inspector and write some code in that event handler ?
The following code :
if Assigned(FOnNewTransaction
FOnNewTransaction(Self);
the assigned will only be true if you have added some code in the event handler of the OnNewTransaction event.
One last note, your component has a memory leak. Why, well it is a good practice in Component Buidling to destroy everything you created yourself. In the constructor you instantiate a TStringList and a TTimer. Since the TTimer has an owner it will be cleaned up automatically when the Owner component gets destroyed. The TStringList on the other hand doesn't have an owner and will not be destroyed automatically, thus it will stay in memory causing a memory leak.
You should override the destructor of your component and in your case destroy the TTimer and TStringList in the destructory of your component. ( possibly you did it, but you didnt include the destructor in your code sample ).
Hoping that this will be of any help to you.
Best regards,
Stefaan
ASKER
Thank you vorisek!
I deleted the code inside the OnCreate
procedure of the calling program and it worked!
Thanks again!
I deleted the code inside the OnCreate
procedure of the calling program and it worked!
Thanks again!
ASKER
TOndrej,
I did enable the Timer but have not included it in the sample code.
Stefaan,
I did all that you said in your comment, but again, have not included them in the sample code. Sorry for that. But I appreciate your help.
I did enable the Timer but have not included it in the sample code.
Stefaan,
I did all that you said in your comment, but again, have not included them in the sample code. Sorry for that. But I appreciate your help.