Solved

Arrays of visual components

Posted on 1998-05-27
5
203 Views
Last Modified: 2010-04-06
Is there an easy way to create an array of visual components? I'm going to use a lot of buttons, and I want to arrange them in an array so I don't need to write an eventhandler for each one of them. Is this possible?
0
Comment
Question by:red_2
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1348061
You can use a TList, which contains a list of pointers to objects.
E.g.
MyList := TList.Create;
MyList.Add(Button1);
MyList.Add(Button2);
Etc...

You can then go through the list and set each buttons event handler to the same event.
E.g.
for i := 0 to MyList.Count - 1 do
  TButton(MyList[i]).OnClick := MyClickEvent;

Cheers,
JB
0
 
LVL 4

Expert Comment

by:d003303
ID: 1348062
Yo,
you could also use the components/controls property of the owner/parent component. If you e.g. arrange all buttons on a TPanel, use its controls property array to walk through all buttons. Assign the event handler at run-time like JimBob said. To filter the button that fired the event in the handler, use

procedure TForm1.MyClickEvent(Sender: TObject);
begin
  if Sender is TButton then
   begin
     if TButton(Sender).Caption = // whatever, or use its tag property
   end;
end;

Slash/d003303
0
 
LVL 3

Accepted Solution

by:
rickpet earned 100 total points
ID: 1348063
Simplier way to do this...you don't need array's, list or anything to hold the components...Also you can use the tag value to make each of your buttons unique...ie TButton(Controls[i]).tag := 0;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('MyClick');
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
(*this loops through all the children of Form1, you could easily make this a panel or groupbox*)
  for i:= 0 to ControlCount -1 do
  begin
(*We check the Controls[i] array, which holds the children of our component i.e. Form, panel or groupbox, why make another array when one is already made for you...*)
    if Controls[i] is TButton then
      TButton(Controls[i]).OnClick := Button1Click;
  end;
end;

Rick
0
 
LVL 4

Expert Comment

by:d003303
ID: 1348064
rickpet,
thx to re-fomulate my comment to your answer.
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1348065
d003303 Had nothing to do with your answer...I was typing up code...never saw your comment...

Rick
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
automatically logging in via bde delphi 3 70
Convert Jpg /PNG To GIF 5 120
add combobox item based on numbers 9 123
Multi-layered image in FireMonkey 9 16
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now