Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create an array of buttons at runtime Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
26-Aug-02
Category
VCL-General
Language
Delphi All Versions
Views
100
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas 

How to create an array of buttons at runtime

Answer:

Here is a unit that creates a row of buttons and a label at run time and displays 
which button is clicked on. All you need to do is start a new project, then paste 
all the code below into Unit1.

1   unit Unit1;
2   
3   interface
4   
5   uses
6     SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, 
7   Dialogs, StdCtrls;
8   
9   type
10    TForm1 = class(TForm)
11      procedure FormCreate(Sender: TObject);
12      procedure ButtonClick(Sender: TObject);
13    private
14      { Private declarations }
15    public
16      { Public declarations }
17    end;
18  
19  var
20    Form1: TForm1;
21  
22  implementation
23  
24  {$R *.DFM}
25  
26  const
27    b = 4; {Total number of buttons to create}
28  
29  var
30    ButtonArray: array[0..b - 1] of TButton; {Set up an array of buttons}
31    MessageBox: TLabel;
32  
33  procedure TForm1.FormCreate(Sender: TObject);
34  var
35    loop: integer;
36  begin
37    {Size the form to fit all the components in}
38    ClientWidth := (b * 60) + 10;
39    ClientHeight := 65;
40    MessageBox := TLabel.Create(Self); {Create a label...}
41    MessageBox.Parent := Self;
42    MessageBox.Align := alTop; {...set up it's properties...}
43    MessageBox.Alignment := taCenter;
44    MessageBox.Caption := 'Press a Button';
45    for loop := 0 to b - 1 do {Now create all the buttons}
46    begin
47      ButtonArray[loop] := TButton.Create(Self);
48      with ButtonArray[loop] do
49      begin
50        Parent := self;
51        Caption := IntToStr(loop);
52        Width := 50;
53        Height := 25;
54        Top := 30;
55        Left := (loop * 60) + 10;
56        Tag := loop; {Used to tell which button is pressed}
57        OnClick := ButtonClick;
58      end;
59    end;
60  end;
61  
62  procedure TForm1.ButtonClick(Sender: TObject);
63  var
64    t: Integer;
65  begin
66    t := (Sender as TButton).Tag; {Get the button number}
67    MessageBox.Caption := ' You pressed Button ' + IntToStr(t);
68  end;
69  
70  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC