Reputation: 2110
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 TPanel
s 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.
Upvotes: 4
Views: 234
Reputation: 4902
There's a fourth way, by taking advantage of the absolute
keyword.
procedure Tmain.pStarting1MouseEnter(Sender: TObject);
var
Panel: TPanel absolute Sender;
begin
if Sender is TPanel then
Panel.BevelInner := bvLowered;
end;
Upvotes: 1
Reputation: 108948
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;
Upvotes: 11