[fpc-pascal] Assigning events in objects

Michael Van Canneyt michael at freepascal.org
Wed Feb 3 20:59:42 CET 2010



On Wed, 3 Feb 2010, Zitt Zitterkopf wrote:

> All,
> I'm writing a TurboVision App to cross compile to Linux - but debugging on my main Win32 box.
>  
> In this app; I'm attempting to use the TFPTimer component to trigger an event every 1000ms. I can't get the bugger to compile; despite google searches and a lot of
> experience in Delphi.

You are mixing a Delphi Class (TFPTimer) and a TV object (Tapplication). You can never mix them.
You can use them independent of each other in the same application, but not assign a tp-style 
method to a delphi-style event handler.

You need to use a Delphi class TApplication object. If that is not possible, what you can do is 
create a go-between class:

TMyEvent = Class(TObject)
   Procedure MyEventHandler( Sender : TObject );
   MyApplication : TApplication
end;

Procedure TMyEvent.MyEventHandler(Sender : TObject );

begin
  MyApplication.TimerTimer(Sender);
end;

And assign this to the timer event:

  constructor TBootApp.Init;
  BEGIN
   FmyHandler:=TMyEvent.create;
   FMyhandler.MyApplication:=Self;
   aTimer := TFPTimer.Create(nil);
   aTimer.Interval := 500; //ms
   aTimer.Enabled := False;
   aTimer.OnTimer := @FMyHandler.MyEventHandler;
  END;

Michael.

>  
> The compiler always seems to throw an error simular to:
>  
> TVBOOT.PAS(52,20) Error: Incompatible types: got "<procedure variable type of   procedure(TObject) of object;Register>" expected "CLASSES.<procedure variable type of
> procedure(TObject) of object;Register>"
> 
> I began by using the advice of Graeme in the post:
> http://readlist.com/lists/lists.freepascal.org/fpc-pascal/0/3897.html
>  
> I know this is simple compiler gymnastics; but I just can't seem to figure it out. 
> John
>  
> uses
> CRT,App,Objects,Menus,Drivers,Views,Dialogs,
> fpTimer;
>  
> type
>  TBootApp = object(TApplication)
>  private
>   aTimer : TfpTimer;
>  protected
>   constructor Init;
>   destructor Done; virtual;
>   procedure InitStatusLine; virtual;
>   procedure HandleEvent(var Event: TEvent); virtual;
>   procedure Idle; virtual;
>   function NewDialog : word;
>  public
>   procedure TimerTimer( Sender : TObject );
>  end;
>  PDemoDialog = ^TDemoDialog;
>  TDemoDialog = object(TDialog)
>  end;
>  
> var
>  BootApp : TBootApp;
>  Dialog  : PDemoDialog;
>  DoDialog : Boolean;
> 
> constructor TBootApp.Init;
> BEGIN
>  aTimer := TFPTimer.Create(nil);
>  aTimer.Interval := 500; //ms
>  aTimer.Enabled := False;
>  aTimer.OnTimer := @TimerTimer;   //compiler barfs <<<---- here!
> END;
> 
> destructor TBootApp.Done;
> BEGIN
>  ATimer.Free;
> END;
>  
> procedure TBootApp.TimerTimer( Sender : TObject );
> BEGIN
> END;
> 
>


More information about the fpc-pascal mailing list