2013-12-07 24 views
2

我在Delphi中創建了我自己的組件(它像一個可以移動的按鈕),安裝它。然後我創建一個新的項目,並從那裏添加新的項目,並添加了幾個新的組件元素。但只有最後一個添加是能夠移動!其他沒有。爲什麼會發生?我怎麼修復它?只有最後添加的德爾福組件執行操作

這裏的組件代碼:

unit ModifiedButton; 

interface 

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
    StdCtrls, ExtCtrls; 


var Timer: TTimer; 

type 
    TSpeed = (Slow,Normal,Fast); 
    TModifiedButton = class(TButton) 
    private 
    { Private declarations } 
    FCount:integer; 
    Velocity:integer; 
    FSpeed:TSpeed; 

    protected 
    { Protected declarations } 
    procedure Click;override; 
    procedure Move(Vel:Integer); 
    procedure OnTimer(Sender: TObject); 
    public 
    { Public declarations } 
    procedure ShowCount; 
    published 
    { Published declarations } 
    property Count:integer read FCount write FCount; 
    property Speed: TSpeed read FSpeed write FSpeed; 

    constructor Create(aowner:Tcomponent); override; 

    end; 

procedure Register; 

implementation 

procedure Register; 
begin 
    RegisterComponents('MyComponents', [TModifiedButton]); 
end; 

{ TModifiedButton } 

procedure TModifiedButton.Click; 
begin 
    inherited Click; 
    FCount:=FCount+1; 
    if (Speed = Slow) then 
    Velocity:=2; 
    if (Speed = Normal) then 
    Velocity:=10; 
    if (Speed = Fast) then 
    Velocity:= 20; 
    Timer.Enabled:=True; 


end; 

constructor TModifiedButton.Create(aowner: Tcomponent); 
begin 
    inherited Create(aowner); 
    Timer:=TTimer.Create(self); 
    Timer.Enabled:=false; 
    Timer.OnTimer:=OnTimer; 
    Timer.Interval:=10; 

end; 

procedure TModifiedButton.Move(Vel: Integer); 
begin 
    Left:=Left + Vel; 
end; 

procedure TModifiedButton.OnTimer(Sender: TObject); 
begin 
    Move(Velocity); 
end; 

procedure TModifiedButton.ShowCount; 
begin 
    ShowMessage('You cliked '+ caption+' for '+inttostr(FCount)+' times'); 
end; 

end 

回答

8

由於Timer是全局變量,因此您創建的每個新按鈕都將覆蓋上一個按鈕的OnTimer事件處理程序。解決方案,使計時器成爲您的TModifiedButton類的成員:

unit ModifiedButton; 

interface 

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
    StdCtrls, ExtCtrls; 

type 
    TSpeed = (Slow,Normal,Fast); 
    TModifiedButton = class(TButton) 
    private 
    { Private declarations } 
    FCount:integer; 
    Velocity:integer; 
    FSpeed:TSpeed; 
    Timer: TTimer; 

    protected 
    { Protected declarations } 
    procedure Click;override; 
    procedure Move(Vel:Integer); 
    procedure OnTimer(Sender: TObject); 
    public 
    { Public declarations } 
    procedure ShowCount; 
    published 
    { Published declarations } 
    property Count:integer read FCount write FCount; 
    property Speed: TSpeed read FSpeed write FSpeed;  
    constructor Create(aowner:Tcomponent); override; 
    destructor Destroy; override;  
    end; 

procedure Register; 

implementation 

{ TModifiedButton } 

procedure TModifiedButton.Click; 
begin 
    inherited Click; 
    FCount:=FCount+1; 
    Case Speed of 
    Slow : Velocity:=2; 
    Normal : Velocity:=10; 
    Fast : Velocity:= 20; 
    end; 
    Timer.Enabled:=True; 
end; 

procedure TModifiedButton.Move(Vel: Integer); 
begin 
    Left:=Left + Vel; 
end; 

procedure TModifiedButton.OnTimer(Sender: TObject); 
begin 
    Move(Velocity); 
end; 

procedure TModifiedButton.ShowCount; 
begin 
    ShowMessage(Format('You clicked %s for %d times', [Caption, FCount])); 
end; 

constructor TModifiedButton.Create(AOwner: Tcomponent); 
begin 
    inherited Create(AOwner); 
    Timer := TTimer.Create(self); 
    Timer.Enabled:=false; 
    Timer.OnTimer:=OnTimer; 
    Timer.Interval:=10; 
end; 

destructor Destroy; 
begin 
Timer.Enabled := False; 
Timer.Free; 
end; 

procedure Register; 
begin 
    RegisterComponents('MyComponents', [TModifiedButton]); 
end; 

end. 
+0

哦謝謝!該死的我認爲,如果它是組件單元,它應該像每個領域都是本地的 – DanilGholtsman