Recent

Author Topic: get the form's name when a procedure is called  (Read 7571 times)

Gald

  • Full Member
  • ***
  • Posts: 107
get the form's name when a procedure is called
« on: June 09, 2021, 06:30:39 am »
Hello! I'm looking for the easiest way to get the form's name when a procedure is called without the need to pass it when calling it.
Example:

Code: Pascal  [Select][+][-]
  1. // In form 999999
  2. procedure TForm999999.Button3Click(Sender: TObject);
  3. begin    
  4.   MyFunction('abc');
  5. end;
  6.  
  7. // In Unit Commun
  8. procedure MyFunction(aText: String);
  9. var
  10.  aOwner: TWinControl;
  11. begin
  12.    aOwner: GetFormNameWhoCalledThisProcedure;
  13.   //code...
  14. end;

Can someone help me?
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: get the form's name when a procedure is called
« Reply #1 on: June 09, 2021, 08:56:07 am »
The more obvious solution would be to redeclare the MyFunction as:
Code: Pascal  [Select][+][-]
  1. procedure MyFunction(Sender: TObject; aText: String);
  2. var
  3.   F: TCustomForm;
  4. begin
  5.   if Sender is TControl then F := GetParentForm(TControl(Sender))
  6.   //code...
  7. end;

Bart
« Last Edit: June 09, 2021, 08:58:13 am by Bart »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: get the form's name when a procedure is called
« Reply #2 on: June 09, 2021, 10:14:58 am »
A little more complete example:
Code: Pascal  [Select][+][-]
  1. // In form 999999
  2. procedure TForm999999.Button3Click(Sender: TObject);
  3. begin    
  4.   MyFunction(Self, 'abc');
  5. end;
  6.  
  7. // In Unit Commun
  8. procedure MyFunction(Sender: TObject; aText: String);
  9. var
  10.   F: TCustomForm;
  11.   FormName: String;
  12. begin
  13.   if Sender is TControl then begin
  14.     F := GetParentForm(TControl(Sender));
  15.     FormName := F.Name; { in case you want it for anything else }
  16.     ShowMessageFmt('Called with %s from form %s', [aText, FormName]);
  17.   end;
  18.   { rest of code }
  19. end;
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.

Gald

  • Full Member
  • ***
  • Posts: 107
Re: get the form's name when a procedure is called
« Reply #3 on: June 09, 2021, 06:33:29 pm »
Thank you both, guys.
But is there some idea of how to do it without the need to pass as a parameter when calling it?
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get the form's name when a procedure is called
« Reply #4 on: June 09, 2021, 06:41:55 pm »
No.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: get the form's name when a procedure is called
« Reply #5 on: June 09, 2021, 06:53:38 pm »
But is there some idea of how to do it without the need to pass as a parameter when calling it?

No, you have to have some kind of access to the control/form, otherwise, how else would you know who called? It's like someone calling to your door: she has to be there when you look through the peephole or open the door; if nobody is there you can assume some hellion just rung for the heck of it, but you don't really know.
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.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: get the form's name when a procedure is called
« Reply #6 on: June 09, 2021, 06:58:39 pm »
In theory, it is possible.

One can rewrite the compiler, makes it automatically records the last visual component that make a call. But still, it is extremely hard and almost impossible because a function can be called form a formless program, for example from console mode or a thread or whatever. For a bounty of thousands dollars, I think still no one will want to take the challenge.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: get the form's name when a procedure is called
« Reply #7 on: June 09, 2021, 07:03:08 pm »
If you really are not allowed to change the calling parameters of MyFunction you still can introduce a global variable which you set to the form before calling MyFunction, and which you reset afterwards or inside Myfunction:

Code: Pascal  [Select][+][-]
  1. var
  2.   Caller: TForm;
  3.  
  4. procedure TForm999999.Button3Click(Sender: TObject);
  5. begin    
  6.   Caller := self;
  7.   MyFunction('abc');
  8.   Caller := nil;
  9. end;
  10.  
  11. procedure MyFunction(aText: String);
  12. begin
  13.   if Caller <> nil then
  14.     ShowMessage('Text "' + aText + '" sent by ' + Caller.Name)
  15.   else
  16.     ShowMessage('Text "' + aText + '" sent by nobody.');
  17. end;
  18.  
« Last Edit: June 09, 2021, 07:07:37 pm by wp »

Gald

  • Full Member
  • ***
  • Posts: 107
Re: get the form's name when a procedure is called
« Reply #8 on: June 09, 2021, 07:52:30 pm »
It is possible.
For a bounty of thousands dollars, I think still no one will want to take the challenge.

KEWK!  :P

You still can introduce a global variable which you set to the form before calling MyFunction.

200 IQ!
Many thanks.
« Last Edit: June 09, 2021, 07:54:09 pm by Gald »
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: get the form's name when a procedure is called
« Reply #9 on: June 10, 2021, 06:11:17 pm »
You can iterate the stack frames to see where the call is coming from.
Never done that, but should be possible.
(The same happens when an exception occurs)

Bart

 

TinyPortal © 2005-2018