English 中文(简体)
尽量减少/避免重复 法典
原标题:Minimalising/Optimising Repetitious Code

我的代码为“一致”TPanel,条件是 mo曲被 h弃。 我也有一套法典,可以加以统一。

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条<代码>。 TPanels to animate, that will mean another 27 Line of repetitous Code. 因此,我可以选择这样做吗?

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.

最佳回答
if Sender is TPanel then
  TPanel(Sender).BevelInner := bvLowered;

或者,如果您确信Sender总是TPanel,则简单明了。

(Sender as TPanel).BevelInner := bvLowered;

or (if you are really certain)

TPanel(Sender).BevelInner := bvLowered;
问题回答

第四个办法是利用absolute

procedure Tmain.pStarting1MouseEnter(Sender: TObject);
var
    Panel: TPanel absolute Sender;

begin
    if Sender is TPanel then
        Panel.BevelInner := bvLowered;
end;




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签