Mikayil Abdullayev
Mikayil Abdullayev

Reputation: 12367

Delphi Component Assign Event on the fly

I have an ADOStoredProc on my form. It's not visual but in code.Normally it's pretty easy to handle an event if a component is visual.It's just a matter of double clicking the desired event. But how do I do it with code.I've declared a procedure:

 procedure SP_SearchAfterScroll(DataSet:TDataSet)

Now how do I assign SP_Search(this is the ADOStoredProc) AfterScroll event handler property to the procedure I wrote above. I'm sure you're going to answer it. So thanks in advance.

Upvotes: 3

Views: 1378

Answers (2)

Thomas Riedel
Thomas Riedel

Reputation: 71

Declare as:

TDataSetNotifyEvent

  • then it works

Upvotes: 0

Marjan Venema
Marjan Venema

Reputation: 19346

When SP_Search is the TAdoStoredProc and has an OnAfterScroll property, all you need to do is:

SP_Search.OnAfterScroll := SP_SearchAfterScroll;

I am assuming that you used the correct signature for SP_SearchAfterScroll. That is to say that the OnAfterScroll property has a type looks like:

TScrollEvent = procedure(DataSet: TDataSet) of object;

If the OnAfterScroll property has a type that differs from this, you will need to make sure that your SP_SearchAfterScroll procedure matches the parameters in that type.

Edit

In the comments Mikayil asked

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?

I hadn't gotten round to answering that and in the mean time Mikey explained it very well, so for (easier) future reference I am including his explanation up here:

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.

Upvotes: 11

Related Questions