Accessing an event in a DataModule from another form

In Delphi 2009, I have a form with the MyProcedure procedure, which is written to the shortcut in the form. The form uses a DataModule with a ClientDataSet. When the AfterScroll event for the ClientDataSet fires, MyProcedure must be executed. To avoid circular references and more important ones, since I want the DataModule to be reused, the DataModule should not reference this particular form.

In short, I hope that I can access the AfterScroll event from my form. Can I connect an Afterscroll event to a DataModule from my form? I thought it was possible, but I don’t remember how to do it. Thanks in advance.

+3
source share
3 answers

You put the event property in your DataModule:

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

Then you call this event in the AfterScroll procedure in the DataModule:

If Assigned(FOnAfterScroll) then FOnAfterScroll(Self);

In the form: declare an event handler

procedure HandleAfterScroll(Sender : TObject);

Then you assign the procedure DataModule OnAfterScroll

Datamodule1.OnAfterScroll: = MyHandleAfterScroll;

Another way would be to send a custom Windows message from the DataModule and reply to this message in the form.

+6
source

It should be something like:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DataModule1.MyCDS.AfterScroll := MyAfterScrollHandler;
end;
+2
source

, , , . , , . , .

, :

type
  TMyEvent = procedure({arg list here}) of object;

  TMyDataModule = class(TDataModule)
  //definition goes here
    procedure MyTableAfterScroll({arg list here});
  private
    FExternalEvent: TMyEvent;
  public
    property ExternalEvent: TMyEvent read FMyEvent write FMyEvent
  end;

implementation

procedure TMyDataModule.MyTableAfterScroll({arg list here});
begin
  //do whatever
  if assigned(FExternalEvent) then
    FExternalEvent({whatever arguments});
  //do more stuff, if you'd like
end;

To enable it, in the OnCreate form, simply assign your procedure MyDataModule.ExternalEvent, and you will be well off.

+1
source

Source: https://habr.com/ru/post/1709025/


All Articles