6
votes

I have a procedure named XYZ(sender:TObject) in delphi. There is one button on my form.

Button.onclick:= xyz;
Button.OnExit:= xyz;

Both the events calls the same procedure. I want to determine in procedure XYZ, which event calls this(onclick or onexit) and according to that proceed with coding. How to determine which event gets fired? thanks

1
If you have such needs, probably you have too much code in xyz. You should split it, in x, y e z. Probably OnClick will call x, OnExit will call y, and both will call z.Mad Hatter
The more general question is: How can I get a "call stack" in Delphi? - see this question Need a way to periodically log the call stack/stack trace for EVERY method/procedure/function calledmjn
@mjn That wouldn't really help here. Surely you don't won't to encode VCL private implementation details into VCL client code?David Heffernan
@David of course your answer is the way to go - call stack parsing would be the least acceptable solution, I would not dare posting it as an answer ;)mjn
This sound like broken logic of code reuse. Call stack is more debugging tool rather than design tool.OnTheFly

1 Answers

11
votes

You can't get hold of that information by fair means. The solution is to use two separate top-level event handlers which in turn can call another method passing a parameter identifying which event is being handled.

type
  TButtonEventType = (beOnClick, beOnExit);

procedure TMyForm.ButtonClick(Sender: TObject);
begin
  HandleButtenEvent(beOnClick);
end;

procedure TMyForm.ButtonExit(Sender: TObject);
begin
  HandleButtenEvent(beOnExit);
end;

procedure TMyForm.HandleButtonEvent(EventType: TButtonEventType);
begin
  //use EventType to decide how to handle this
end;