Link to home
Start Free TrialLog in
Avatar of pin_plunder
pin_plunder

asked on

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.
Avatar of mhervais
mhervais

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


ASKER CERTIFIED SOLUTION
Avatar of aubs
aubs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Avatar of pin_plunder

ASKER

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.