There are two objects: Button1, Image1. I want to click on the picture, the same procedure was performed as when pressing the button. How to implement:?

procedure TProForm.Button1Click(Sender: TObject); begin MessageBox(handle,'НаТали Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ! НаТмитС Π½Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΡƒ','Π‘Π”',mb_ok); end; procedure TProForm.Image1Click(Sender: TObject); begin end; 

You can, of course, just copy everything that is in the first procedure, and what if the code is huge, what to do?

    2 answers 2

    Everything is very simple

     procedure TProForm.Click(Sender: TObject); var myBtn: TButton; myImg: TImage; begin if Sender is TButton then begin myBtn := (Sender as TButton); //ΠΊΠΎΠ΄, Ссли Π½Π°ΠΆΠ°Ρ‚Π° ΠΊΠ½ΠΎΠΏΠΊΠ° end; if Sender is TImage then begin myImg := (Sender as TImage); //ΠΊΠΎΠ΄, Ссли Π½Π°ΠΆΠ°Ρ‚Π° ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ° end; //ΠΎΠ±Ρ‰ΠΈΠΉ ΠΊΠΎΠ΄ end; 

    And in the Events editor you can point to this procedure for both objects. Or do it dynamically, for example in the OnFormCreate method:

     myButton.onClick := self.Click; myImage.onClick := self.Click; 

    If my memory serves me, then just like that, there is no delphi at hand to check.

    UPD

    And you can also use your method like this:

     procedure TProForm.CommonClick; begin //ваш Π΄Π»ΠΈΠ½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ end; procedure TProForm.Button1Click(Sender: TObject); begin self.CommonClick; end; procedure TProForm.Image1Click(Sender: TObject); begin self.CommonClick; end; 

    Or mine:

     procedure TProForm.Click(Sender: TObject); var myBtn: TButton; myImg: TImage; procedure DoSomthing; begin //ваш Π΄Π»ΠΈΠ½Π½Ρ‹ΠΉ ΠΊΠΎΠ΄ end; begin if Sender is TButton then begin myBtn := (Sender as TButton); DoSomthing; end; if Sender is TImage then begin myImg := (Sender as TImage); DoSomthing; end; end; 
    • So instead of // bla bla, do I need to write the same code in two cases? or after two conditions to write the procedure code? Not quite understand the logic of this method. - chuikoff
    • You may have something different, depending on the object, but something to be joint. This is just an example; you can remove these conditions altogether if you have a completely identical code. - Dex

    On the Events tab of your image in OnClick, put your buttonclick

    • "TPersistens to help" - specify what you mean? - Kromster