Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Buttons 4

Status
Not open for further replies.

Oxidized

Programmer
Nov 20, 2000
8
KR
Hello people.
I would like to know if there's any way to add buttons in a form but inside an array.
So I can do something like this, for instance,

for c1:=1 to 5
do
Button[1].enabled:=true;

In VB was pretty easy, U just copy and paste the button
and the program asked U if U wanted to make an array.
But I can't find the way to do it in Delphi.

Thanx in advance.

Rusty.

PS:Yes, I'm very sorry I programmed in VB.
 
would creating the buttons inside a container object work
By doing this the buttons get the same properties as the container
 
Hi

As an old VB programmer this was one of the first things I wanted to learn. Copy and paste this onto a newly created delphi project and run it. Object arrays (Control arrays in VB) are possible in Delphi they are just a bit harder to create. Anyway have fun with this.

//**********************************************************
unit Unit1;

interface

uses //Include stdctrls in the uses clause
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
stdctrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//Create a procedure for the button's OnClick
procedure MyButtonOnClick(Sender : TObject);
end;

var
Form1: TForm1;
//Initialise Button array;
btn : array[1..10] of TButton;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var x : ShortInt;
begin
for x:=1 to 10 do
begin
//Create the buttons
btn[x]:=TButton.Create(Self);
with btn[x] do
begin
parent:=Form1;
Height:=25;
width:=75;
Left:=20;
Top:=25+((x*25)+1);
Caption:='Button '+IntToStr(x);
Name:='btn'+IntToStr(x);
//Assign the OnClick to your MyButtonOnClick procedure
OnClick:=MyButtonOnClick;
end;
end;
end;

procedure TForm1.MyButtonOnClick(Sender : TObject);
begin
//Using the Sender you can tell which button has been clicked on
ShowMessage('You Clicked '+ (Sender as TButton).Caption);
end;
end.
//**********************************************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top