[workaround found] How to get the sender within a frame? lazarus / 21-General ---------------------------------------------------------------------- Nicole · 2025-07-02 08:04 There is a method, which I shifted into a frame: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ruft die Berichterstellung und übergibt damit Modus Tagprocedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin   Berichte_erstellen(self);end; Bericht = report in English The problem: "self" does not point at the calling Speedbutton any more, but on the frame, which holds it. The thing generates different reports dependent to the button, which calls it. It would be easy to give a parameter alike 'daily report' or so. Unfortunately the click event does now allow to pass parameters other than sender. Or does it? How to solve it? Yes, I can write a calling rountirne, but this becomes more and more ugly. Is there a smart solution? What I need: A information of which button a certain report is asked for. ---------------------------------------------------------------------- Fibonacci · 2025-07-02 08:10 Am I missing something? Sender is the button, Self is the frame containing it. ---------------------------------------------------------------------- rvk · 2025-07-02 08:12 Berichte_erstellen isn't an event so you can change it to receive multiple parameters. So: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ruft die Berichterstellung und übergibt damit Modus Tagprocedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin   Berichte_erstellen(self, Sender);end; In Berichte_erstellen you can examine the Sender and act accordingly. ---------------------------------------------------------------------- Nicole · 2025-07-02 08:15 I read "wrong number of parameters" and if I use a dot instead of a comma "does not have such a member". ---------------------------------------------------------------------- rvk · 2025-07-02 08:18 Quote from: Nicole on July 02, 2025, 10:15:41 amI read "wrong number of parameters" and if I use a dot instead of a comma "does not have such a member". I said you needed to change the definition of the procedure Berichte_erstellen. Did you do that??? Berichte_erstellen doesn't seem to be a click event so you could change it. (if it is a event then you need to show a lot more code) So: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Berichte_erstellen(Sender: TObject; ButtonPressed: TObject); ---------------------------------------------------------------------- Thaddy · 2025-07-02 08:38 Isn't it simply: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ruft die Berichterstellung und übergibt damit Modus Tagprocedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin   Berichte_erstellen(sender);end; ---------------------------------------------------------------------- Nicole · 2025-07-02 08:43 @Thaddy, thank you for the solution, but this does not work, I tried before. The sender is "TSpeedButton", but not the specific speed button. In the meanwhile, I change it to a combination of all and special rvk's inspirations. If I must change the function, I can change sender to string as well. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // ruft die Berichterstellung und übergibt damit Modus Tagprocedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin   Berichte_erstellen('TagesberichtErstellen');end; works. ---------------------------------------------------------------------- Martin_fr · 2025-07-02 08:49 "Self" should never have been the speedbutton. (Unless you had subclassed TMySpeedbutton = class(TSpeedButton) ) "Self" most likely (almost certain) was the TForm. "Sender" should be the clicked speedbutton. If not, then you must have something very unexpected somewhere else. ---------------------------------------------------------------------- Nicole · 2025-07-02 09:10 oh yes, this had worked for years. After the re-shaping of panels and frames, I clicked the usual button and - nothing happened any more. This "Sender" always has been mysteriously to me. It does not behave in the usual way parameters do. And just deleting it brings troubles sometimes. I bet it comes from the stone-age of programming. ---------------------------------------------------------------------- rvk · 2025-07-02 09:19 Quote from: Nicole on July 02, 2025, 11:10:58 amIt does not behave in the usual way parameters do.Sure it does. It's just that it you want to know who called the event and want to use it's parameters you need to cast it to the correct class. Quote from: Nicole on July 02, 2025, 11:10:58 amAnd just deleting it brings troubles sometimes.Why would you delete it? It's part of the defined procedures in event. It's a parameter so you know who called the event. You can delete is but then you also need to change a lot more from where it's called, usually a doclick in the inherited class which you then need to override. But it's not advised (even if you don't use the sender in your event handler). Quote from: Nicole on July 02, 2025, 11:10:58 amI bet it comes from the stone-age of programming. Nope, you just need to know why it's passed and how to use it. I guess you skipped those parts of all the beginner guides you needed to read ---------------------------------------------------------------------- egsuh · 2025-07-02 09:25 Sender is TObject. So, if you want to do anything meaningful, you have to type case it. For example, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin    ShowMessage((Sender as TSpeedButton).caption);      // or you may write as follows if you are reading caption, as property Caption is available from TControl in the inheritance tree.      ShowMessage((Sender as TControl).caption);     Berichte_erstellen(Sender);end; So if the Sender of Berichte_erstellen is always TSpeedButten, then you can define and call as : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure  Berichte_erstellen(SpeedButton:TSpeedButton);begin      with SpeedButton do begin          // do Something;       end;end; // And calll  procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin    Berichte_erstellen(Sender as TSpeedButton);end; OR, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure  Berichte_erstellen(SpeedButton:TObject);begin      with SpeedButton as TSpeedButton do begin             // do Something     end; end; // And calll  procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin    Berichte_erstellen(Sender);end; ---------------------------------------------------------------------- Thaddy · 2025-07-02 09:44 You can put that in concrete by also usingCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if sender is TSpeedButton then I have to test that for actions, though: I always program through actions when writing a user interface and that does not work: Sender is TAction, not TSpeedButton. You need to be aware of that. ---------------------------------------------------------------------- creaothceann · 2025-07-02 10:15 Quote from: Thaddy on July 02, 2025, 11:44:44 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if sender is TSpeedButton thenWould silently fail if some other component is calling that function by mistake. ---------------------------------------------------------------------- egsuh · 2025-07-02 10:23 Quote I always program through actions when writing a user interface and that does not work: Sender is TAction, not TSpeedButton. Hmmm... I think I had experienced the same issue, and defined at ButtonClick, etc. instead of Action. I needed to recognize which button called the event handler. ---------------------------------------------------------------------- Thaddy · 2025-07-02 10:24 The name.... With actions that is basically irrelevant, otherwise: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  TSpeedButton(sender).name ---------------------------------------------------------------------- n7800 · 2025-07-02 11:53 Both TAction and TSpeedButton are TComponent, so for both can be used (although it is advisable to check via "is"): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TComponent(Sender).Name  In general, for debugging, the ClassName property of the TObject itself can be used: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Sender.ClassName ---------------------------------------------------------------------- cdbc · 2025-07-02 11:59 Hi @Nicole: You just need to study 'PolyMorphism' in the context of classes. Basically you can stuff any descendant of 'TObject' into the '(Sender: TObject)' parameter. The 'As' & 'Is' operators are made for dealing with PolyMorphism (the ability to assume more forms or shapes). as @Thaddy*) correctly wrote. Regards Benny eta: *) and n7800 ---------------------------------------------------------------------- Thaddy · 2025-07-02 12:09 @n7800 OP's specification is which button, not which button class. ---------------------------------------------------------------------- Nicole · 2025-07-02 13:14 now it is about sports  "TComponent(Sender).Name" is a good idea, but will not work as sender, because this gives a string, not an object "if....is..." is good an idea, but does not change under the line the value passed And yes, I read the beginners stuff about senders, loooong ago. And forgot it all in the meanwhile. If sender is alike an usual parameter, pls tell me: - Why does every button click event enforce this parameter, -  mostly not used? - Why must I set in the preferences, that "not used" is not shown in this case? - Why can't I add an additional parameter to a button-click-event or change it? - What is this "sender" good for, if this idiot of a button, does not even know his own name? ---------------------------------------------------------------------- Thaddy · 2025-07-02 13:52 sender IS the object instance....It happens to have a property called "name".... (You might think a little bit longer about how things actually work...  ) It is not a type, it is the instance, so you can manipulate all properties and events. Provided the proper cast. ---------------------------------------------------------------------- rvk · 2025-07-02 14:04 Quote from: Nicole on July 02, 2025, 03:14:17 pm- Why does every button click event enforce this parameter, -  mostly not used?Because it's the most useful parameter in a click event You can use the same event for multiple classes (pointing to the same click event) and your onclick can then determine via Sender from which one it comes from. You can use ClassName to determine that and after that you can use TClass(Sender).xxx to access everything from that sender. Quote from: Nicole on July 02, 2025, 03:14:17 pm- Why can't I add an additional parameter to a button-click-event or change it?You can. But if you want to do that, you need to override the DoClick procedure of the component first to add the parameter in the "if assigned(fonclick) do fonclick(sender, myparam);". Quote from: Nicole on July 02, 2025, 03:14:17 pm- What is this "sender" good for, if this idiot of a button, does not even know his own name? It does know it's own name. Why do you think it doesn't? What exactly do you want to accomplish? Maybe show some code. I think it can be done much much simpler than what you are trying to do. ---------------------------------------------------------------------- Nicole · 2025-07-02 15:25 @rvk This was exactly the problem! I wanted to do what you suggest: Use one method from several buttons called. After having put those buttons into a frame, they did not "know their names any more". They said the name of the frame instead of their own. After fuzzing around, they "knew more": they said, they were "SpeedButtons", - but not which one! All the ideas suggested in this thread I have tried before, because they sound logic. They did not work. Before posting, I tried for at least an hour to make those buttons passing their own names to the function (what they did fine, before having been shifted to this frame). Be aware, if you need 4 lines of code to pass just a parameter, this is no solution any more. And one more sender bashing: Why can't I see in the watch-list the sender's value? I remember this was the same trouble in Delphi. I stuck to it: Sender is not my favourite. ---------------------------------------------------------------------- rvk · 2025-07-02 17:34 Quote from: Nicole on July 02, 2025, 05:25:23 pm@rvk This was exactly the problem! I wanted to do what you suggest: Use one method from several buttons called. After having put those buttons into a frame, they did not "know their names any more". They said the name of the frame instead of their own. Ha, now I know where you are going wrong in your reasoning. You do understand Sender is a TObject and TObject is actually just a pointer?? It hold the base class of all classes. So it can be anything. But... TObject doesn't have a property "name". That is only introduced in TComponent. https://www.freepascal.org/docs-html/rtl/classes/tcomponent.html So... although TObject can POINT to a TButton, you can't access it until you actually cast it to TButton (or TComponent as it's the first time .name is introduced). So consider (and test) the following code: It checks it the Sender is a TComponent (or descendant) and only then shows you the TComponent.Name property. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  if Sender is TComponent then    Showmessage(TComponent(Sender).Name);end; Does that make it more clear. So you can create one event handler, attach it to lots of different TComponent descendants (TButton, TForm, TSpeedButton etc) and with just this code you can retrieve the .Name. You can also do other things if you know the class Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);begin  if Sender is TComponent then    Showmessage(TComponent(Sender).Name);  if Sender is TForm then    Showmessage(TForm(Sender).Name + ' has a left property of ' + TForm(Sender).Left.ToString);end; Try it... connect this Button1Click (I didn't rename it so you can just reuse the same event) to the Form (by the pulldown choosing Button1Click for the OnClick in the Form OnClick event). When clicking the TForm (just click anywhere on the form) you first get the name (because TForm is also a TComponent) and then you get the "Form1 has a left property of 123". I hope this clarify some things. ---------------------------------------------------------------------- Nicole · 2025-07-02 17:59 Thank you for the explanation. To repeat this, is a good idea. ---------------------------------------------------------------------- n7800 · 2025-07-02 20:45 Quote from: Thaddy on July 02, 2025, 02:09:39 pm@n7800 OP's specification is which button, not which button class. As I mentioned, I meant for debugging purposes only. Calling ClassName is easier than listing all possible class variants via "if Sender is ... then ... else if Sender is ...". ---------------------------------------------------------------------- n7800 · 2025-07-02 20:57 Quote from: Nicole on July 02, 2025, 05:25:23 pmThis was exactly the problem! I wanted to do what you suggest: Use one method from several buttons called. After having put those buttons into a frame, they did not "know their names any more". Wait, I don't get it... If you added a button to a frame, and the frame to your form, how do you want to recognize the button by name? There can be any number of frames in a form, and inside each there will be buttons with the same names... Quote from: Nicole on July 02, 2025, 05:25:23 pmThey said the name of the frame instead of their own. I don't understand how Sender could return a frame instead of a button? Quote from: Nicole on July 02, 2025, 05:25:23 pmAfter fuzzing around, they "knew more": they said, they were "SpeedButtons", - but not which one! What "fuzzing around"? How did they say? It seems my translator can't handle it )) It would be better if you actually attached a simple project of what you want to achieve. ---------------------------------------------------------------------- Remy Lebeau · 2025-07-03 01:56 Quote from: Nicole on July 02, 2025, 05:25:23 pmThis was exactly the problem! I wanted to do what you suggest: Use one method from several buttons called. After having put those buttons into a frame, they did not "know their names any more". They said the name of the frame instead of their own. That is because you redesigned your UI but did not pay attention to your new hierarchy or to what the new Self actually points at.  You are not asking for the name of the button at all.  You are asking for the name of the Frame instead. Self is a hidden pointer inside of a class method. It points at an instance of the class which owns that method. SpeedButton_TagesberichtErstellenClick() is a method of the TFrame_BerichteF class, and therefore its Self pointer always points at an instance of TFrame_BerichteF.  It does not matter what kind of object event SpeedButton_TagesberichtErstellenClick() is assigned to.  Its Self will always point at the owning TFrame_BerichteF object. That's why events have a Sender parameter, to point at the object which is triggering the event. This flexibility is what allows multiple object events to be assigned a shared event handler, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SpeedButton1.OnClick := SpeedButton_TagesberichtErstellenClick;SpeedButton2.OnClick := SpeedButton_TagesberichtErstellenClick;... Or for multiple objects to assign their own handler to a shared event, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if SomeCondition then  SpeedButton1.OnClick := Frame1.SpeedButton_TagesberichtErstellenClickelse if SomeOtherCondition then  SpeedButton1.OnClick := Frame2.SpeedButton_TagesberichtErstellenClickelse ... Quote from: Nicole on July 02, 2025, 05:25:23 pmAfter fuzzing around, they "knew more": they said, they were "SpeedButtons", - but not which one! Sender is a pointer.  It points at the object which triggered the event.  You can use that pointer to access members of that object, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin  ShowMessage(TSpeedButton(Sender).Name);end; Or even to just compare it to object pointers directly, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);begin  if Sender = SpeedButton1 then    ...  else if Sender = SpeedButton2 then    ...end; Quote from: Nicole on July 02, 2025, 05:25:23 pmAll the ideas suggested in this thread I have tried before, because they sound logic. They did not work. Yes, they do work. You are just not applying them correctly to your situation. Quote from: Nicole on July 02, 2025, 05:25:23 pmWhy can't I see in the watch-list the sender's value? Because the Sender is itself just a raw TObject pointer, so all you can see from it is a memory address and no properties. To see anything more meaningful, you have to type-cast it to the correct derived type in order to access its properties. Quote from: Thaddy on July 02, 2025, 11:44:44 amI always program through actions when writing a user interface and that does not work: Sender is TAction, not TSpeedButton. Then cast the Sender to TAction and use its ActionComponent property to know which component triggered the action. ---------------------------------------------------------------------- egsuh · 2025-07-03 05:43 You should be able to identify the Sender, if OnClick event is defined (not Action). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     procedure TFrame_BerichteF.SpeedButton_TagesberichtErstellenClick(Sender: TObject);    begin         if Sender = SpeedButton1 then  Berichte_erstellen('Daily Report')         eles if Sender = SpeedButton2 then Berichte_erstellen('Weekly Report')         eles if Sender = SpeedButton3 then Berichte_erstellen('Monthly Report')       eles ShowMessage (Sender.className + ' should not call me');     end; If this code does "ShowMessage", then please let us know Sender.className. ---------------------------------------------------------------------- creaothceann · 2025-07-03 06:31 You could also assign IDs to the Tags and do this: Code: [Select]procedure TFrame_BerichteF.SpeedButton_OnClick_TagesberichtErstellen(Sender : TObject); begin case (Sender as TComponent).Tag of 1:   Berichte_erstellen('Daily'   + ' Report'); 2:   Berichte_erstellen('Weekly'  + ' Report'); 3:   Berichte_erstellen('Monthly' + ' Report'); else raise Exception.Create(Sender.ClassName + ' should not call me'); end; end; Quote from: egsuh on July 03, 2025, 07:43:05 amIf this code does "ShowMessage" As it is now it'll show a compilation error