Solved

Arrays of visual components

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

Technology Partners: 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

734 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