Using 'this' in Delphi

0

Just as there is 'this' in JavaScript, what is the equivalent in Delphi? And how would I use it? I want to replace the 'UniButtonX' of each procedure with a 'this' equivalent to the clicked button.

procedure TUniForm1.UniButton1Click(Sender: TObject);
var numero : string;
begin
   numero := UniEdit1.Text;
   UniEdit1.Text := numero + UniButton1.Caption;
end;

procedure TUniForm1.UniButton2Click(Sender: TObject);
var numero : string;
begin
   numero := UniEdit1.Text;
   UniEdit1.Text := numero + UniButton2.Caption;
end;
    
asked by anonymous 14.09.2017 / 18:55

1 answer

1

I achieved ... it's the 'sender'.

procedure TUniForm1.UniButton1Click(Sender: TObject);
var numero : string;
begin
   numero := UniEdit1.Text;
   UniEdit1.Text := numero + TUniButton(sender).Caption;
end;

procedure TUniForm1.UniButton2Click(Sender: TObject);
var numero : string;
begin
   numero := UniEdit1.Text;
   UniEdit1.Text := numero + TUniButton(sender).Caption;
end;

But I still have the doubt as to how I could create a function for when to call it in the OnClick of each button, execute it with the 'sender' of the clicked button.

I got it again ...

procedure TUniForm1.clique(Sender: TObject);
var numero : string;
begin
   numero := UniEdit1.Text;
   UniEdit1.Text := numero + TUniButton(sender).Caption;
end;

procedure TUniForm1.UniButton1Click(Sender: TObject);
begin
   clique(TUniButton(sender));
end;

procedure TUniForm1.UniButton2Click(Sender: TObject);
begin
   clique(TUniButton(sender));
end;

If anyone has any suggestions for optimizing the code, please reply / comment. What I'm doing is a calculator in Delphi.

    
14.09.2017 / 19:24