gpt4 book ai didi

Delphi 组件动态分配事件

转载 作者:行者123 更新时间:2023-12-03 15:41:15 26 4
gpt4 key购买 nike

我的表单上有一个 ADOStoredProc。它不是可视的,而是在代码中。通常,如果组件是可视的,则处理事件非常容易。只需双击所需的事件即可。但是我如何用代码来做到这一点。我已经声明了一个过程:

 procedure SP_SearchAfterScroll(DataSet:TDataSet)

现在如何将 SP_Search(这是 ADOStoredProc)AfterScroll 事件处理程序属性分配给我上面编写的过程。我确信你会回答它。所以提前致谢。

最佳答案

当 SP_Search 是 TAdoStoredProc 并且具有 OnAfterScroll 属性时,您需要做的就是:

SP_Search.OnAfterScroll := SP_SearchAfterScroll;

我假设您为 SP_SearchAfterScroll 使用了正确的签名。也就是说,OnAfterScroll 属性的类型如下:

TScrollEvent = procedure(DataSet: TDataSet) of object;

如果 OnAfterScroll 属性的类型与此不同,则需要确保 SP_SearchAfterScroll 过程与该类型中的参数匹配。

编辑

在 Mikayil 提出的评论中

SP_Search.AfterScroll :=
SP_SearchAfterScroll(SPSearch)'
the compiler complains saying incompatible types TNotifyEvent and procedure. But when I write SP_Search.AfterScroll :=
SP_SearchAfterScroll
it works. What's the difference?

我还没来得及回答这个问题,同时Mikey解释得很好,所以为了(更容易)将来引用,我将他的解释放在这里:

SP_Search.AfterScroll := that code assigns a function to handle the event when it fires - you are not making a call to SP_SearchAfterScroll at 'assign time' just assigning a value to a variable, so you don't pass parameter. Parameter is needed when call is made - when event fires then caller will assign parameter with the right value. When you pass the parameter,compiler assumes you are calling the function, not assigning it, so you get incompatible types error. When you simply assign the function without the parameter, compiler understands you're assigning, not calling the function.

关于Delphi 组件动态分配事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6079858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com