6

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

Nalu
  • 1,107
  • 4
  • 20
  • 43
  • 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 Jan 09 '12 at 09:39
  • 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 called](http://stackoverflow.com/questions/2326980/need-a-way-to-periodically-log-the-call-stack-stack-trace-for-every-method-proce) – mjn Jan 09 '12 at 12:06
  • @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 Jan 09 '12 at 12:09
  • @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 Jan 09 '12 at 12:14
  • This sound like broken logic of code reuse. Call stack is more debugging tool rather than design tool. – OnTheFly Jan 09 '12 at 14:05

1 Answers1

11

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;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • is it possible to get that information in same procedure without using another parameter? – Nalu Jan 09 '12 at 10:44
  • 1
    I'm sorry, I don't quite understand that question. – David Heffernan Jan 09 '12 at 10:51
  • can we get event information like which event fired? Just want to know. – Nalu Jan 09 '12 at 12:02
  • No you can't. That's covered by the first sentence of the answer. – David Heffernan Jan 09 '12 at 12:03
  • +1. It's worth pointing out that you could also pass Sender as a parameter, then you'd be able to use this trick on multiple buttons. Say you have 10 buttons and you wanted to be able to handle them all individually (such as maybe modifying captions/icons/etc.) within HandleButtonEvent. You would be able to assign all of their click and exit to the two ButtonClick and ButtonExit routines above, without having to replicate all of them. Obviously, this is only useful if there is a need to handle multiple buttons! – Chris Thornton Jan 09 '12 at 15:46