Board index » delphi » 400 buttons on the form...

400 buttons on the form...

Hello Delphi gurus!

I am wondering whether it is possible to programmatically create and place
200 buttons on a form (with no design-time button creating). What I am
looking for is to create an array of 20 by 20 of buttons and am not sure
how to do something like

MyButton : array [1..20,1..20] of TSpeedButton;

and then I'd like to be able to use loops to manage them for example

for a := 1 to 20 do for b := 1 to 20 do
 MyButton[a,b].Caption := IntToStr(a*b);

Please point me into a direction or better yet if you have a bit of code
I'd appreciate it very much.

Thank you for your help,

Robert

 

Re:400 buttons on the form...


Quote
Robert <u...@together.net> wrote:
>I am wondering whether it is possible to programmatically create and place
>200 buttons on a form (with no design-time button creating). What I am
>looking for is to create an array of 20 by 20 of buttons and am not sure
>how to do something like
>MyButton : array [1..20,1..20] of TSpeedButton;
>and then I'd like to be able to use loops to manage them for example
>for a := 1 to 20 do for b := 1 to 20 do
> MyButton[a,b].Caption := IntToStr(a*b);
>Please point me into a direction or better yet if you have a bit of code
>I'd appreciate it very much.

for i:=1 to 20
  for j:=1 to 20
    sb=new(TSpeedButton).Create(..)
    sb.Left=.. sb.Top=.. ... { actually, there's a call SetBounds or
                               something that sets all bounds at once }
  end
end

But, to be frank, you honestly don't want 400 buttons on a form. It will
take ages to appear, and it will hog system resources. I'm not even sure
that Windows supports more than 256 controls.

The correct way to do it would be to create your own custom control. Just
a single custom control. Paint it however you want, using standard canvas
painting commands. Respond to mouse clicks &c. yourself, with whatever
graphical effects you want.

Who knows... maybe such a sort of component is already part of the VCL?

--
Lucian Wischik, Queens' College, Cambridge CB3 9ET. ljw1...@cam.ac.uk

Re:400 buttons on the form...


Robert,
  The enclose project files show haw to do this.  You would also want to
set some of the other properties for the speedbuttons depending on how you
want them to interreact with each other.  (e.g. AllowAllUp and GroupIndex).
 Don't try to do this with normal TButtons or TBitBtn's as they are
windowed controls and will probably kill your app since they will use to
many of Windows' handles.  TSpeedButton doesn't cause this problem since it
is not a windowed control and therefore does not use a handle.  Also note
that you have to add Buttons to the main uses clause if it is not already
there.

  Just copy the files to a directory and open/compile with D1 or D2.

Hope this helps!  (Project files only sent via direct email.  Sample code
below.)

Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

var
  SB: array [1..20,1..20] of TSpeedButton;

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: Integer;
begin
  for i := 1 to 20 do
    for j := 1 to 20 do
    begin
      SB[i,j]:= TSpeedButton.Create(Form1);
      SB[i,j].Parent := Form1;
      SB[i,j].Font.Name := 'Ariel';
      SB[i,j].Font.Height := 12;
      SB[i,j].Font.Style := [];
      SB[i,j].SetBounds(((i-1) * 20),((j-1) * 20), 20, 20);
      SB[i,j].Caption := IntToStr(i*j);
    end;
end;
--

Robert <u...@together.net> wrote in article
<01bc5edb$f096e180$da362...@ns1.together.net.together.net>...

Quote
> Hello Delphi gurus!

> I am wondering whether it is possible to programmatically create and
place
> 200 buttons on a form (with no design-time button creating). What I am
> looking for is to create an array of 20 by 20 of buttons and am not sure
> how to do something like

> MyButton : array [1..20,1..20] of TSpeedButton;

> and then I'd like to be able to use loops to manage them for example

> for a := 1 to 20 do for b := 1 to 20 do
>  MyButton[a,b].Caption := IntToStr(a*b);

> Please point me into a direction or better yet if you have a bit of code
> I'd appreciate it very much.

> Thank you for your help,

> Robert

Re:400 buttons on the form...


In article <01bc5edb$f096e180$da362...@ns1.together.net.together.net>,
   "Robert" <u...@together.net> wrote:

Quote
>Hello Delphi gurus!

>I am wondering whether it is possible to programmatically create and
place
>200 buttons on a form (with no design-time button creating).

Try this:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var i : integer;
begin
     for i := 1 to 20 do
     begin
        with TButton.create(self) do
        begin
           parent := form1;
           top := i * 10;
           caption := inttostr(i);
        end;
     end;
end;

end.

The buttons click events can be specified by means of index of the
button.

Other Threads