Solved

Arrays of visual components

Posted on 1998-05-27
5
210 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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