Board index » delphi » How to index series of buttons?

How to index series of buttons?

Hi

I have 5 buttons that are not an array of controls.
Is there a way to do the following? I'm using D1.
Or is there a way thru Object Inspector to make a array of buttons?

for i := 1 to 5 do
   form1.button + IntoStr(i) .caption := 'something here'

   Thanks
   Steve
--
Steve Siperas   sipe...@hsi.com          " GO UCONN HUSKIES "
3M Health Information Systems                          --------  __o
Wallingford, Connecticut                              -------  _`\<,_
Give me your dirty love -- Frank Zappa               -------  (*)/ (*)

 

Re:How to index series of buttons?


Consider the following code Steve:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
  StdCtrls;

type
  TArrayButton = array[1..5] of TButton;
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyArrayButton : TArrayButton;
implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  loop, NumOfButtons, NumofComponents : integer;
  ButtonArray : TButton;
begin
  NumofComponents := Form1.ComponentCount;
  NumOfButtons := 0;
  for loop := 0 to NumofComponents - 1 do
  begin
    if (Form1.Components[loop] is TButton) then
    begin
      ButtonArray := (Form1.Components[loop] as TButton);
      NumOfButtons := NumOfButtons + 1;
      ButtonArray.Caption := IntToStr(NumOfButtons) + ' Button';
    end;
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
   MyArrayButton[1] := Button1;
   MyArrayButton[1].Caption := 'Hello';
end;

end.

I have done two things here. Firstly, I have used the component count
property to go through the conponents on the form and count the number of
buttons. I also change the caption of those buttons. Secondly, I have
created an array of TButtons and assigned one of the buttons (Button1) to
this array and changed its caption when I click another button (Button6).

Hope thsi helps you out!!
John

Quote
Steve Siperas wrote:
> Hi

> I have 5 buttons that are not an array of controls.
> Is there a way to do the following? I'm using D1.
> Or is there a way thru Object Inspector to make a array of buttons?

> for i := 1 to 5 do
>    form1.button + IntoStr(i) .caption := 'something here'

>    Thanks
>    Steve
> --
> Steve Siperas   sipe...@hsi.com          " GO UCONN HUSKIES "
> 3M Health Information Systems                          --------  __o
> Wallingford, Connecticut                              -------  _`\<,_
> Give me your dirty love -- Frank Zappa               -------  (*)/ (*)

Re:How to index series of buttons?


Steve,

var
   Btn : TButton;
for i := 1 to 5 do
begin
   Btn := FindComponent('Button' + IntToStr(i));
   if Assigned(Btn) then
   begin
      case i of
         0: Btn.Caption := 'Button 0';
         1: Btn.Caption := 'This is Button 2';
      else
         Btn.Caption := 'This is Button ' + IntToStr(i);
      end;
   end;
end;

Ken
--
Ken White

Clipper Functions for Delphi
http://members.aol.com/clipfunc

Quote
Steve Siperas wrote:

> Hi

> I have 5 buttons that are not an array of controls.
> Is there a way to do the following? I'm using D1.
> Or is there a way thru Object Inspector to make a array of buttons?

> for i := 1 to 5 do
>    form1.button + IntoStr(i) .caption := 'something here'

>    Thanks
>    Steve
> --
> Steve Siperas   sipe...@hsi.com          " GO UCONN HUSKIES "
> 3M Health Information Systems                          --------  __o
> Wallingford, Connecticut                              -------  _`\<,_
> Give me your dirty love -- Frank Zappa               -------  (*)/ (*)

Re:How to index series of buttons?


In article <71vjq2$ic...@mercury.hsi.com>, sipe...@hsi.com (Steve Siperas)
writes:

Quote
>I have 5 buttons that are not an array of controls.
>Is there a way to do the following? I'm using D1.
>Or is there a way thru Object Inspector to make a array of buttons?

>for i := 1 to 5 do
>   form1.button + IntoStr(i) .caption := 'something here'

John Noel has suggested one way using the Components array of the form, which
includes all components whose Owner is the form.

There is another array - the Controls array which contains all controls whose
Parent is the control holding the array. To use this approach :-

Put the five buttons onto a TPanel (you can set the BevelInner and BevelOuter
to bsNone to make the panel invisible) . Then code :-

unit
type
  TMyButtonArray : array[0..4] of TButton;
var
  MyButtonArray : TMyButtonArray;

 . . . then in FormCreate

var
  i : integer
begin
  with Panel1 do begin
    for i := 0 to ControlCount - 1 do
      if (Controls[i] is TButton) then     // just in case it isn't
        MyButtonArray[i] := TButton(Controls[i]);

 . .  then you can do anywhere  :-

     MyButtonArray[2].Caption := 'This One';

Alan Lloyd
alangll...@aol.com

Other Threads