Search code examples
delphipascallazarusfpc

How to repeat the procedure of a button


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 ?


Solution

  • 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;