How can I create a control array?

Well, I believe the title says it all, but though I'll explain myself a little bit more. I'm trying to make a calculator so that every time the user clicks on the numbers (0,1,2,3...9) I want Delphi to call always the same procedure. That procedure would contain a case structure so that it displays in the edit box what number was pressed.

In Visual Basic it would be really almost something like this:

First you create the control arrays and when you double click any button a unique subroutine is created for all.

procedure TfrmCalculus.ButtonClick(Sender: TObject, Index: Integer);
begin
  case Index of
      0:    edtCalculus.Text := edtCalculus.Text + '0';
      1:    edtCalculus.Text := edtCalculus.Text + '1';
      2:    edtCalculus.Text := edtCalculus.Text + '2';
      3:    edtCalculus.Text := edtCalculus.Text + '3';
      4:    edtCalculus.Text := edtCalculus.Text + '4';
      5:    edtCalculus.Text := edtCalculus.Text + '5';
      6:    edtCalculus.Text := edtCalculus.Text + '6';
      7:    edtCalculus.Text := edtCalculus.Text + '7';
      8:    edtCalculus.Text := edtCalculus.Text + '8';
      9:    edtCalculus.Text := edtCalculus.Text + '9';
   end; {end case}
end;

I believe the code is OK, but what i really don't know is how to create the control arrays first to make this code work correctly.

Thanks.
pin_plunderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mhervaisCommented:
1) create a new project
2) on the form of the project put 2 buttons and a TEdit

3) double click on the button one
it will open you the source editor at the place where the clicked event is taken care of

write this method :

procedure TForm1.Button1Click(Sender: TObject);
begin
  if sender.ClassNameIs('TButton') then
  begin
    if TButton(sender).Name ='Button1' then
      Edit1.Text := '1';
    if TButton(sender).Name = 'Button2' then
      Edit1.Text := '2';
  end;
end;

4) go back to your form and select the second button

5) on the object inspector click the event tab and select the onclicked event with a simple click.

6) a dropdown listbox proposes you the
method you have already written :
TForm1.Button1Click(Sender: TObject);

select it, run your program and enjoy

regards, Marc


0
aubsCommented:
Hi pin_plunder

This question about calculators seems to come up time and time again.

If you use the caption property of the buttons you will save alot of typing.
Set the caption of the buttons 0 to 9 and make them all call the same procedure:

procedure TForm1.btn1Click(Sender: TObject);
begin
  EdtDisplay.Text:= EdtDisplay.Text + TButton(sender).Caption;
end;

If you really want to use the same style as VB (WHY?) you could always set the tag property of each button to 0, 1, 2, etc then reference the tag instead like the VB index:

procedure TForm1.btn1Click(Sender: TObject);
Begin
  Case TButton(sender).Tag of
    0:    edtCalculus.Text := edtCalculus.Text + '0';
    1:    edtCalculus.Text := edtCalculus.Text + '1';
    2:    edtCalculus.Text := edtCalculus.Text + '2';
    3:    edtCalculus.Text := edtCalculus.Text + '3';
    4:    edtCalculus.Text := edtCalculus.Text + '4';
    5:    edtCalculus.Text := edtCalculus.Text + '5';
    6:    edtCalculus.Text := edtCalculus.Text + '6';
    7:    edtCalculus.Text := edtCalculus.Text + '7';
    8:    edtCalculus.Text := edtCalculus.Text + '8';
    9:    edtCalculus.Text := edtCalculus.Text + '9';
  end; {end case}
end;

Regards

Aubs
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edeyCommented:
or a slighty more fleshed out (and automated) version :):


unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
const
     kSpace = 3;
var
   ix : integer;
begin
     for ix := 0 to 8 do
     with TButton.create(self) do
     begin
          width := 25;
          left := kSpace+(ix mod 3)*(width+kSpace);
          top := (3-(ix div 3))*(height+kSpace);
          caption := intToStr(ix+1);
          onClick := doBtnClick;
          parent := self;
     end;
end;

procedure TForm1.doBtnClick(Sender: TObject);
begin
     edit1.text := TButton(sender).caption;
end;

end.


GL
Mike


ps. MHervais, didn't really want to "steal" your idea, but merely ment to show how highly similar objects can be created, placed, event's assigned, and reffered to with minimal code & effort when done as a single task
0
mhervaisCommented:
no harm Mike. My concerns was only to give an anwer that works to answer the question in order to demonstrate the topics, not to write the program

regards, Marc
0
pin_plunderAuthor Commented:
I'm sorry mhervais, your answer was good, but aubs comment was better. I mean he/she wrote what i really wanted to know.

Some comments to make things clearer..
I thought of working with the buttons captions to save some lines of code, but what i didn't know how to refer to each button's caption: Tbutton(Sender).Caption.

That little piece of code was what i needed and aubs provided it. So aubs, and everybody, consider this question answered.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.