delphievent-handlingdatamodule

Access an event on a DataModule from another Form


In Delphi 2009 I have a Form with a procedure MyProcedure that writes to a label on the Form. The form uses a DataModule with a ClientDataSet. When the AfterScroll event of the ClientDataSet is fired MyProcedure should be executed. To avoid circular references and more important, as I want the DataModule to be reusable, the DataModule should not reference to this specific Form.

In short, I hope that I can access the AfterScroll event from my Form. Can I hook up the Afterscroll event on the DataModule from my Form? I thought it should be possible, but I cannot remember how to do it. Thanks in advance.


Solution

  • You need to put an event property in your DataModule:

    private
    FOnAfterScroll : TNotifyEvent;
    public
    property OnAfterScroll   : TNotifyEvent read FOnAfterScroll write FOnAfterScroll;
    

    You then call that event in the AfterScroll procedure in the DataModule:

    If Assigned(FOnAfterScroll) then FOnAfterScroll(Self);
    

    In Form: declare event handler

    procedure HandleAfterScroll(Sender : TObject);
    

    Then you assign a procedure to DataModule's OnAfterScroll

     Datamodule1.OnAfterScroll :=
     MyHandleAfterScroll;
    

    Another way would be to send a custom windows message from DataModule and to respond to that message in the Form.