Recent

Author Topic: Component created at runtime: pass a parameter  (Read 766 times)

valter.home

  • Jr. Member
  • **
  • Posts: 81
Component created at runtime: pass a parameter
« on: April 10, 2021, 01:19:49 am »
Is it possible to pass a parameter to an event assigned to a component created at runtime or is it needed to use a global variable?
Example:

Code: Pascal  [Select][+][-]
  1. procedure MyForm.MyEvent((Sender: TObject);
  2. begin
  3.   // do something
  4. end;
  5.  
  6. NewPnl: TPanel;
  7. ...
  8. NewPnl := TPanel.Create(Target);
  9. Newpnl.OnClick:=@MyEvent;
  10.  

Is it possible to send a parameter such as a string type to MyEvent?
I tried but he doesn't accept it.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Component created at runtime: pass a parameter
« Reply #1 on: April 10, 2021, 01:53:16 am »
Is it possible to send a parameter such as a string type to MyEvent?

No, whatever you pass to the event handler, which is in this context just another procedure, has to have the correct type, that is a TObject or descendant (in fact, any object).
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

valter.home

  • Jr. Member
  • **
  • Posts: 81
Re: Component created at runtime: pass a parameter
« Reply #2 on: April 10, 2021, 02:15:35 am »
Thank you.
I have a tPanel which contains a TImage and a TLabel. All three components are created at runtime.
When I click on one of them I should send a path to a procedure to be able to set the root of a TShellTreeView.
I thought of assigning the string "path" to the name of the three components (and read it in the procedure) but obviously it contains the slashes and is not a valid name.
I could use "Caption" for TPanel and TLabel and find a solution for TImage but maybe it is better to use a global variable.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Component created at runtime: pass a parameter
« Reply #3 on: April 10, 2021, 07:24:13 am »
Yes, either a global or a field/property in the form class. It's a little difficult to say without knowing what are you doing and why, or seeing some code but those are two of the more common solutions for that.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

bpranoto

  • Full Member
  • ***
  • Posts: 134
Re: Component created at runtime: pass a parameter
« Reply #4 on: April 10, 2021, 12:32:29 pm »
Thank you.
I have a tPanel which contains a TImage and a TLabel. All three components are created at runtime.
When I click on one of them I should send a path to a procedure to be able to set the root of a TShellTreeView.
I thought of assigning the string "path" to the name of the three components (and read it in the procedure) but obviously it contains the slashes and is not a valid name.
I could use "Caption" for TPanel and TLabel and find a solution for TImage but maybe it is better to use a global variable.

I would create child classes for the components and add the information you want to pass (the path). Then in the event procedure you can read that information.

 

TinyPortal © 2005-2018