从另一个表单访问 DataModule 上的事件

发布于 2024-07-20 15:20:18 字数 342 浏览 9 评论 0原文

在 Delphi 2009 中,我有一个带有过程 MyProcedure 的表单,该过程写入表单上的标签。 该表单使用带有 ClientDataSet 的 DataModule。 当 ClientDataSet 的 AfterScroll 事件被触发时,应该执行 MyProcedure。 为了避免循环引用,更重要的是,因为我希望 DataModule 可重用, DataModule 不应引用此特定表单。

简而言之,我希望可以从我的表单访问 AfterScroll 事件。 我可以从我的表单中连接 DataModule 上的 Afterscroll 事件吗? 我认为这应该是可能的,但我不记得该怎么做。 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蘑菇王子 2024-07-27 15:20:18

您需要在 DataModule 中放置一个事件属性:

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

然后在 DataModule 的 AfterScroll 过程中调用该事件:

If Assigned(FOnAfterScroll) then FOnAfterScroll(Self);

在表单中:
声明事件处理程序

procedure HandleAfterScroll(Sender : TObject);

然后将过程分配给 DataModule 的 OnAfterScroll

 Datamodule1.OnAfterScroll :=
 MyHandleAfterScroll;

另一种方法是从 DataModule 发送自定义窗口消息并在表单中响应该消息。

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.

安人多梦 2024-07-27 15:20:18

应该是这样的:

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

Should be something like:

procedure TForm1.FormCreate(Sender: TObject);
begin
  DataModule1.MyCDS.AfterScroll := MyAfterScrollHandler;
end;
凉宸 2024-07-27 15:20:18

如果您只想在不同的单元(例如表单)中声明事件处理程序,请遵循 Ulrich 的建议。 如果您希望能够将默认事件处理程序放入数据模块中,但随后能够扩展其行为,则需要做更多的工作。 您可以通过向数据模块添加事件来完成此操作。

使用适当的签名定义一个方法指针,并将其添加到公共范围的数据模块中,如下所示:

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;

要连接它,在表单的 OnCreate 中,只需将您的过程分配给 MyDataModule.ExternalEvent 即可。

If all you want is to declare the event handler in a different unit, like the form, go with Ulrich's suggestion. If you want to be able to put a default event handler in your data module but then be able to extend its behavior, it takes a bit more work. You can do this by adding an event to the data module.

Define a method pointer with the appropriate signature and add one to the data module at public scope, like so:

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 hook it up, in your form's OnCreate, just assign your procedure to MyDataModule.ExternalEvent and you'll be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文