Recent

Author Topic: How can I determine which dynamically created label I clicked on?  (Read 3543 times)

Ziska

  • New member
  • *
  • Posts: 7
Hi!
I’ve tried doing this in different ways, but I can’t find a way for it to work.
I have an array of TLabel called Termin which goes from 0 to length of another array. The Labels in array Termin are created dynamically. I want to open a new form when I click on a label.
Code: [Select]
for f:=0 to high(Termin) do
     begin
     Termin[f].OnClick := @Form4.ShowDetails;
     end;
This is the procedure which opens, when I click on a label:
Code: [Select]
procedure TForm4.ShowDetails(Sender:TObject);
begin
  Alldays1 := i; //Alldays1 and Alldays2 are sent to form5 to show the contents of a record
  Alldays2 := f;
  Form5.Show;
end;
When I run the program and click on a label, form5 opens and shows the contents of a record, that belongs to Termin[highTermin].
This makes sense, because the program runs f from 0 to high(Termin). When I click on a label, f is already high(Termin). Does anybody know how I can tell the program, which label I clicked on?

Thank you for you help!

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How can I determine which dynamically created label I clicked on?
« Reply #1 on: April 06, 2012, 08:13:08 pm »
Probably Sender parameter will return it.
« Last Edit: April 06, 2012, 08:19:28 pm by typo »

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: How can I determine which dynamically created label I clicked on?
« Reply #2 on: April 06, 2012, 08:14:35 pm »
I guess several options are available.  One of the ideas is to fill the "tag" property of each label you create with a - unique - number, for instance the index of your loop.  When the ShowDetails function is invoked the Sender parameter will be the label that caused the click.  You can then inspect its tag property and determine which one it is.
1.0/2.6.0  XP SP3 & OS X 10.6.8

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: How can I determine which dynamically created label I clicked on?
« Reply #3 on: April 06, 2012, 08:16:01 pm »
In TForm4.ShowDetails, TLabel(Sender).name returns the name you assigned to the button. A better solution is to assign a number to TLabel.Tag at creation and read that as TLabel(Sender).Tag. Integers are easier for comparisons and case statements. Tags are not used anywhere else.

Ziska

  • New member
  • *
  • Posts: 7
Re: How can I determine which dynamically created label I clicked on?
« Reply #4 on: April 06, 2012, 10:22:15 pm »
Thank you so much! I used Tags and it worked!  :D

 

TinyPortal © 2005-2018