1

I have developed two procedures of two buttons to for task 1 and task 2. Do you know how to create the new button which can repeat the procedures of two previous buttons to perform task 1 + 2 in assigned number of times ?

Arioch 'The
  • 15,799
  • 35
  • 62
HD Ly
  • 45
  • 2
  • 5
  • 1
    Use [`actions`](http://wiki.freepascal.org/TActionList) for this purpose. – TLama Jan 13 '14 at 14:55
  • "*repeat ... task 1 + 2 in assigned number of times*" means you press 2 times Button1 (->Task1) and 3 times Button2 (->Task2) and on Button3 you want to execute `Task1; Task1; Task2; Task2; Task2;`? – Sir Rufo Jan 13 '14 at 15:15
  • @TLama: how do actions help here? – jpfollenius Jan 13 '14 at 15:28
  • @jpfollenius, you can make action for each task; instead of methods `DoTask1`, `DoTask2` from the answer here you'll make actions and the code you put into their `OnExecute` event methods... Then you can bind those actions directly to buttons and manually call `Execute` for those actions wherever you want. – TLama Jan 13 '14 at 15:36

1 Answers1

2

Extract the tasks into separate methods:

procedure TForm1.DoTask1;
begin
  ....
end;

procedure TForm1.DoTask2;
begin
  ....
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoTask1;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DoTask2;
end;

And then add a new button with OnClick handler like this:

procedure TForm1.Button3Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 1 to N do
  begin
    DoTask1;
    DoTask2;
  end;
end;
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 2
    +1 this is often a good idea anyway since (a) method names tend to be much more precise and informative and (b) we can change signature, get rid of the `Sender` parameter etc. – jpfollenius Jan 13 '14 at 15:24