Recent

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

valter.home

  • Jr. Member
  • **
  • Posts: 62
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.

jamie

  • Hero Member
  • *****
  • Posts: 4444
Re: Component created at runtime: pass a parameter
« Reply #1 on: April 10, 2021, 01:44:25 am »
The SENDER is the one that called the event..

so if you are calling an event from other controls instead of the one it was originally created in you can test the SENDER for who it is..

For example

 If Sender is TSomeClassOFMine Then with TSomeClassOfMine(Sender) do begin
 
    // access any special properties or variables now;
 End;
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 3870
Re: Component created at runtime: pass a parameter
« Reply #2 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: 62
Re: Component created at runtime: pass a parameter
« Reply #3 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: 3870
Re: Component created at runtime: pass a parameter
« Reply #4 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

  • Jr. Member
  • **
  • Posts: 86
Re: Component created at runtime: pass a parameter
« Reply #5 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