-1

I have a couple of Buttons and Edits and want to get their names into two procedures but can not figure out how. I have set the respective event into OnClick.

  1. TButton L1_1 and TButton L1_2 with event OnClick = L1_1Click. Both triggers procedure TForm1.L1_1Click(Sender: TObject);
  2. Tedit: Edit1 and Edit2 with event OnClick = CheckNamesClick. Both triggers procedure TForm1.CheckNamesClick(Sender: TObject);

When running I am not getting the item name. It only shows TButton'' and TEdit

Can anyone give me a hint?

Regards Peter Sweden

Peter F
  • 29
  • 4
  • 4
    Have a read on [How to access a property of the component that the Sender parameter is referencing](https://stackoverflow.com/a/63209309/4299358) and [How to identify the Tobject type for sender](https://stackoverflow.com/a/5833409/4299358) – AmigoJack Jul 23 '21 at 17:11

1 Answers1

0

From a TButton OnClick event handler, you can get the button name using this code:

procedure TForm1.L1_1Click(Sender: TObject);
begin
    Memo1.Lines.Add((Sender as TButton).Name);
end;

procedure TForm1.CheckNamesClick(Sender: TObject);
begin
    Edit1.Text := (Sender as TEdit).Name;
end;
fpiette
  • 11,983
  • 1
  • 24
  • 46
  • Working fine with Memo and I converted it to a ListBox: ListBox1.items.add((Sender as TButton).Name); Many thanks!! Any suggestion for the second question? I did try: Edit1.items.add((Sender as TEdit).Name); But gives an error for "items" – Peter F Jul 23 '21 at 17:55
  • @PeterF `TEdit` is a single-line control, it doesn't have an `Items` property. Use its `Text` property instead: `Edit1.Text := (Sender as TEdit).Name;` – Remy Lebeau Jul 23 '21 at 17:59
  • An edit control has a single string to display, not an array of items like a list box. Do you want to append to the edit box's text? If so, `Edit1.Text := Edit1.Text + (Sender as TEdit).Name`. – Andreas Rejbrand Jul 23 '21 at 18:00
  • 6
    It's worth adding that this question is often the first step in a naive and *terribly wrong design strategy* where the junior developer believes that a component's `Name` property is something they need to use to reference a control at runtime (it's not), simply because they're used to using that name *in their code* for the same purpose at design time. The next step is for the code to start branching on the sender's `.Name` rather than structuring the code in a sane way to begin with. @PeterF Beware... – J... Jul 23 '21 at 21:14
  • Thank you for your information! I believe there is many smart programming ways to do things, but as I have learnt myself I have not had any teacher to show me smart solusions. I have only searched the internet to try to find the solution if I had problems. I would very much like to learn Delphi the right way, so please inform me where I should start. – Peter F Jul 24 '21 at 08:55
  • SO is not a site where we are allowed to teach a language in general. Instead, you should [ask question](https://stackoverflow.com/questions/how-to-ask). We're more than happy to help once you've made an effort to solve the problem yourself and run into difficulties. When that happens, you can explain the problem you're having, include the relevant portions of your code (and any original code) in the form of a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), and ask a specific question related to that code, and we'll try to help. – fpiette Jul 24 '21 at 11:57
  • And if you want to learn, when you ask a question, don't do like you have done here: you forgot to tell us **why** you are asking. So we cannot really put you on the right track: we don't know where you want to go! The good way to ask it to explain your need and expose a solution you already found. The we'll be able to really help you. – fpiette Jul 24 '21 at 11:59
  • @fpiette I am sorry if I have done something wrong here! As a newbee I don't have the knowledge to put down my questions. You said "**... don't do like you have done here: you forgot to tell us why you are asking"**. If you look at my start I wrote: "**When running I am not getting the item name. It only shows TButton'' and TEdit Can anyone give me a hint?"**. Please inform me HOW I should write my question so I can do better in the future. Thank You! – Peter F Jul 24 '21 at 13:21
  • You said you want to get the button name. OK, I answered. As @J... said in his comment, this is likely a design error. But we really don't know because we don't know **why** you need to get the name of a button. You can't do much with it except very special cases. Usually you only use the field representing a button, like L1_1 in your case (By the way, a very bad naming). – fpiette Jul 24 '21 at 14:39