home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / DELPHI.SWG / 0042_Array of Buttons.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1996-02-21  |  2.4 KB  |  86 lines

  1. {
  2.  
  3. Here is a unit that creates a row of buttons and a label at run time and
  4. displays which button is clicked on. Thanks go to a number of people who pushed
  5. me in the right direction. Like all things in programing 'it's obvious when you
  6. know how'!
  7.  
  8. All you need to do is start a new project, then paste all the code below
  9. into Unit1.
  10.  
  11. -------------------------------------------------------------------------------------
  12. }
  13.  
  14. unit Unit1;
  15.  
  16. interface
  17.  
  18. uses
  19.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  20.   Forms, Dialogs, StdCtrls;
  21.  
  22. type
  23.   TForm1 = class(TForm)
  24.     procedure FormCreate(Sender: TObject);
  25.     procedure ButtonClick(Sender: TObject);
  26.   private
  27.     { Private declarations }
  28.   public
  29.     { Public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.DFM}
  38.  
  39. const
  40.   b = 4;  {Total number of buttons to create}
  41.  
  42. var
  43.   ButtonArray : Array[0..b-1] of TButton; {Set up an array of buttons}
  44.   MessageBox: TLabel;                     {...and a label!}
  45.  
  46. procedure TForm1.FormCreate(Sender: TObject);
  47.   var
  48.     loop : integer;
  49. begin
  50.   ClientWidth:=(b*60)+10;               {Size the form to fit all the}
  51.   ClientHeight:=65;                     {components in.}
  52.  
  53.   MessageBox:=TLabel.Create(Self);      {Create a label...}
  54.   MessageBox.Parent:=Self;
  55.   MessageBox.Align:=alTop;              {...set up it's properties...}
  56.   MessageBox.Alignment:=taCenter;
  57.   MessageBox.Caption:='Press a Button';
  58.  
  59.   for loop:= 0 to b-1 do                {Now create all the buttons}
  60.       begin
  61.         ButtonArray[loop]:=TButton.Create(Self);
  62.         with ButtonArray[loop] do       {Note the use of the with command.}
  63.           begin                            {This lets you leave out the first}
  64.             Parent  :=self;             {bit of the description and}
  65.             Caption :=IntToStr(loop);   {(I think) makes the code easier}
  66.             Width   :=50;               {to read.}
  67.             Height  :=25;
  68.             Top        :=30;
  69.             Left    :=(loop*60)+10;
  70.             Tag        :=loop;         {Used to tell which button is pressed}
  71.             OnClick :=ButtonClick;    {The important bit!}
  72.           end;
  73.       end;
  74. end;
  75.  
  76. procedure TForm1.ButtonClick(Sender: TObject);
  77.   var
  78.     t : Integer;
  79. begin
  80.   t:=(Sender as TButton).Tag;        {Get the button number}
  81.   MessageBox.Caption:='You pressed Button '+IntToStr(t);
  82. end;
  83.  
  84. end.
  85.  
  86.