?
Solved

Arrays of visual components

Posted on 1998-05-27
5
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

718 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