gpt4 book ai didi

delphi - 最小化/优化重复代码

转载 作者:行者123 更新时间:2023-12-03 15:18:27 28 4
gpt4 key购买 nike

这是我的代码,当鼠标光标悬停在 TPanel 上时,它会“动画化”它。我还有一个代码块来取消它的动画。

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;

正如您所看到的,它非常重复,并且由于我还有另外 27 个 TPanel 需要制作动画,这意味着另外 27 行重复代码。那么有什么办法可以优化这个吗?

我还尝试将该代码块放入单独的过程中(在同一单元中)。但 Delphi 告诉我 sender 未声明。

最佳答案

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

或者,如果您确定 Sender 始终是 TPanel,只需

(Sender as TPanel).BevelInner := bvLowered;

或者(如果您真的确定)

TPanel(Sender).BevelInner := bvLowered;

关于delphi - 最小化/优化重复代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663962/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com