0
我創建一個組件併爲其添加一個Tbutton。 現在我想爲我的Component創建OnClick事件,該事件在用戶在運行時單擊我的組件的Button時執行 我該怎麼做?使用組件內的按鈕的Onclick事件
我創建一個組件併爲其添加一個Tbutton。 現在我想爲我的Component創建OnClick事件,該事件在用戶在運行時單擊我的組件的Button時執行 我該怎麼做?使用組件內的按鈕的Onclick事件
@ LU_RD的答案可能是你在找什麼。
我寫了一個較小的例子,應該與您所做的相似。
interface
TMyComponent = class(TCustomControl)
private
embeddedButton: TButton;
fOnButtonClick: TNotifyEvent;
procedure EmbeddedButtonClick(Sender: TObject);
protected
procedure DoEmbeddedButtonClick; virtual;
public
constructor Create(AOwner: TComponent); override;
published
property OnButtonClick: TNotifyEvent read fOnButtonClick write fOnButtonClick;
end;
implementation
// Attach embedded button event handler onto embedded button
constructor TMyComponent.Create(AOwner: TComponent);
begin
// .. other code
embeddedButton.OnClick := EmbeddedButtonClick;
// .. more code
end;
// EmbeddedButtonClick fires internal overridable event handler;
procedure TMyComponent.EmbeddedButtonClick(Sender: TObject);
begin
// If you want to preserve the Sender, extend this method
// with a sender argument.
DoEmbeddedButtonClick;
end;
procedure TMyComponent.DoEmbeddedButtonClick;
begin
// Optionally if you need to do additional internal work
// when the button is clicked, you can do it here.
// Check if event handler has been assigned
if Assigned(fOnButtonClick) then
begin
// Fire user-assigned event handler
fOnButtonClick(Self);
end;
end;
很好非常感謝你**先生。安德森**你的代碼非常有用 – Fayyaz
這是一個非常寬泛的問題。如果你想要一個好的答案,請縮小它的範圍。此外,如果您向其他用戶顯示您迄今嘗試的內容,則此網站的效果最佳... – Kris
「我創建了一個組件,併爲其添加了一個Tbutton。」這是非常模糊的。請提供詳細信息,包括足夠的代碼以供我們理解。這意味着你需要花更多的時間來處理你的問題。我的經驗法則是,你花更多時間寫一個好問題,反應越好,學到的東西越多。 –
請參見[OnClick事件處理程序控制自定義組件中的控件不工作](http://stackoverflow.com/q/23046743/576719)。 –