Solved

Arrays of visual components

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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