Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimalising/Optimising Repetitious Code

Here is my code to "animate" the TPanel when the mouse cursor is hovered over it. I also have a code block to unanimate it.

procedure Tmain.pStarting1MouseEnter(Sender: TObject);
begin
  if sender = pStarting1 then pStarting1.BevelInner := bvLowered;
  if sender = pStarting2 then pStarting2.BevelInner := bvLowered;
  if sender = pStarting3 then pStarting3.BevelInner := bvLowered;
  if sender = pStarting4 then pStarting4.BevelInner := bvLowered;
  if sender = pStarting5 then pStarting5.BevelInner := bvLowered;
  if sender = pStarting6 then pStarting6.BevelInner := bvLowered;
  if sender = pStarting7 then pStarting7.BevelInner := bvLowered;
  if sender = pStarting8 then pStarting8.BevelInner := bvLowered;
  if sender = pStarting9 then pStarting9.BevelInner := bvLowered;
end;

As you can see, its very repetitious and since I have another 27 TPanels to animate, that would mean another 27 lines of repetitious code. So is there a way I can optimise this?

I have also tried placing that block of code into a separate procedure (in the same unit). but Delphi tells me that sender is undeclared.

like image 753
ple103 Avatar asked Dec 16 '22 23:12

ple103


1 Answers

if Sender is TPanel then
  TPanel(Sender).BevelInner := bvLowered;

or, if you are certain that Sender is always a TPanel, simply

(Sender as TPanel).BevelInner := bvLowered;

or (if you are really certain)

TPanel(Sender).BevelInner := bvLowered;
like image 83
Andreas Rejbrand Avatar answered Dec 28 '22 07:12

Andreas Rejbrand