Jamie L. Mitchell wrote:
> Here is what I want, however.  I would like to create this component
> such that it will allow me to attach a different event handler
> signature.  For example, I want to define my new event as follows:
> 
> TjmFileRefBtnPressedEvent = procedure(Sender: TObject; var FileRec:
>                            Integer; var FileName: String) of object;
> 
> I would like to assign this to the button click instead of the
> TNotifyEvent (the compiler absolutely hates this, since the procedure
> signature is different.)  

As well it shouldn't. The types are wrong, so even if the compiler 
allowed it, the button control would have no way of calling that method. 
It only knows how to call one-argument method pointers, not 
three-argument method pointers.

> What kind of magic do I need to do to get this to work?  Ideally,
> inside my component, some kind of code will act as a moderator
> between the original TSpeedButton.OnClick event and the new
> TjmFileRefBtnPressedEvent.  If no event handler is assigned, pressing
> the button would do nothing.  If I assign a handler, such as 
> 
> Procedure WorkWithFileRepository(Sender: TObject; Var Key: Integer; 
>                                  Var Fil: String);
> 
> then pressing the button would execute this method.

Define a private method of your wrapper class to match the TNotifyEvent 
signature. Assign that method to the speed button's OnClick event. 
Within that method, call the three-argument event handler that you 
expose to users of your wrapper component.

type
   TJMComponent = class(TControl)
   private
     FButton: TSpeedButton;
     FOnButtonClick: TJMFileRefButtonEvent;
     procedure HandleButtonClick(Sender: TObject);
   protected
     procedure DoButtonClick(var FileRec: Integer; var FileName: 
string); virtual;
   public
     constructor Create(AOwner: TComponent); override;
   published
     property OnButtonClick: TJMFileRefButtonEvent read FOnButtonClick 
write FOnButtonClick;
   end;

constructor TJMComponent.Create(AOwner: TComponent);
begin
   inherited;
   FButton := TSpeedButton.Create(Self);
   FButton.OnClick := HandleButtonClick;
end;

procedure TJMComponet.HandleButtonClick(Sender: TObject);
var
   FileRec: Integer;
   FileName: string;
begin
   Assert(Sender = FButton);
   FileRec := 0;
   FileName := '';
   DoButtonClick(FileRec, FileName);
   // etc.
end;

procedure TJMComponent.DoButtonClick(var FileRec: Integer; var FileName: 
string);
begin
   if Assigned(OnButtonClick) then
     OnButtonClick(Self, FileRec, FileName);
end;

-- 
Rob

Reply via email to