Recent

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

Gald

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

Bart

  • Hero Member
  • *****
  • Posts: 4361
    • 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: 4177
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

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

Handoko

  • Hero Member
  • *****
  • Posts: 4318
  • 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: 4177
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: 4318
  • 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: 8583
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 »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Gald

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

jamie

  • Hero Member
  • *****
  • Posts: 4752
Re: get the form's name when a procedure is called
« Reply #9 on: June 09, 2021, 11:27:07 pm »
You see, this is where a REF on return to an object would allow this because the function's return would be pointing to the object not just passing back a newly created one.

Function MyFUnction(S:String):Var TObject_base;

ect

The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 4361
    • Bart en Mariska's Webstek
Re: get the form's name when a procedure is called
« Reply #10 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