Recent

Author Topic: Problem with adding an own function to an event  (Read 864 times)

Tomi

  • Full Member
  • ***
  • Posts: 130
Problem with adding an own function to an event
« on: March 04, 2024, 03:55:00 pm »
Hello!

The question is if I try to add an own function to an event of a component, how can I find out the required parameters of that function?
For example, I have created a ScrollBar at runtime in my program, and I would like assign an own function to this OnScroll event:
Code: Pascal  [Select][+][-]
  1. myscrollbar:=TScrollBar.Create(self);
  2.                 myscrollbar.parent:=self;
  3.                 myscrollbar.kind:=sbVertical;
  4.                 myscrollbar.left:=(Form1.width div 2)-4;
  5.                 myscrollbar.top:=buttonAd.height+8;
  6.                 myscrollbar.Height:=Form1.height-buttonAd.height-8;
  7.                 myscrollbar.OnScroll:=@scrolling; //ERROR HERE because of missing paremeters!
The declaration of this function:
Code: Pascal  [Select][+][-]
  1. procedure scrolling(Sender: TObject; var ScrollPos: Integer);
And when I would use it:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.scrolling(Sender: TObject; var ScrollPos: Integer);
  2. begin
  3. (...)
  4. end;
But I got this error message because missing parameters above:
unit1.pas(211,40) Error: Incompatible types: got "<procedure variable type of procedure(TObject;var LongInt) of object;Register>" expected "<procedure variable type of procedure(TObject;TScrollCode;var LongInt) of object;Register>"

Zvoni

  • Hero Member
  • *****
  • Posts: 2744
Re: Problem with adding an own function to an event
« Reply #1 on: March 04, 2024, 04:00:03 pm »
But I got this error message because missing parameters above:
unit1.pas(211,40) Error: Incompatible types: got "<procedure variable type of procedure(TObject;var LongInt) of object;Register>" expected "<procedure variable type of procedure(TObject;TScrollCode;var LongInt) of object;Register>"

Quote
unit1.pas(211,40) Error: Incompatible types:
got
"<procedure variable type of procedure(TObject;var LongInt) of object;Register>"

expected
"<procedure variable type of procedure(TObject;TScrollCode;var LongInt) of object;Register>"
Spot the Difference.....

Quote
The question is if I try to add an own function to an event of a component, how can I find out the required parameters of that function?
Easy.
Place a Control/Component on your form, in the Object-Inspector switch to its Events, and create the Default-Event for whichever OnXXXX-Event you're interested in
« Last Edit: March 04, 2024, 04:04:55 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Чебурашка

  • Hero Member
  • *****
  • Posts: 586
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: Problem with adding an own function to an event
« Reply #2 on: March 04, 2024, 04:19:17 pm »
The question is if I try to add an own function to an event of a component, how can I find out the required parameters of that function?

IIRC, if you CTRL+Click on the class you are interested (in this case TScrollBar), Lazarus should open the source and show you the list of members, sometimes it could happen that is declared in a parent class, but when you find the declaration you should also see how the member is supposed to be. I found the TScrollEvent using this procedure.

Code: Pascal  [Select][+][-]
  1.   TScrollEvent = procedure(Sender: TObject; ScrollCode: TScrollCode;
  2.                            var ScrollPos: Integer) of object;
  3.  
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

Bart

  • Hero Member
  • *****
  • Posts: 5469
    • Bart en Mariska's Webstek
Re: Problem with adding an own function to an event
« Reply #3 on: March 04, 2024, 05:41:05 pm »
If this event is part of your form, then just do Ctrl+Shift+Left-Click on "@scrolling" and CodeTools will create the event definition and an empty method body for you.

Bart

Tomi

  • Full Member
  • ***
  • Posts: 130
Re: Problem with adding an own function to an event
« Reply #4 on: March 05, 2024, 09:17:27 am »
Thank you all for your helpful advice!
Based on Cheburaska's addition, my code is already working! Before this, I tried the trick with CTRL + Left click, but I'm lost in the information Lazarus posted.  %)

dseligo

  • Hero Member
  • *****
  • Posts: 1412
Re: Problem with adding an own function to an event
« Reply #5 on: March 05, 2024, 12:27:17 pm »
If this event is part of your form, then just do Ctrl+Shift+Left-Click on "@scrolling" and CodeTools will create the event definition and an empty method body for you.

Do you mean Shift+Ctrl+C when cursor is on '@scrolling'?
For me Ctrl+Shift+Left-Click creates multiple carets.

 

TinyPortal © 2005-2018