簡體   English   中英

如何在執行期間調用在新組件中創建的過程

[英]How to invoke a procedure created inside the New Component during the implementation

我創建並實現了一個新組件,在這個創建的組件內部,有一個過程InitCombo需要在實現中調用。

我將如何做到這一點?

這是新組件中的InitCombo過程:

procedure TNewComponent.InitCombo;       //TComboBox ancestor
begin
  FStoredItems.OnChange := nil;
  StoredItems.Assign(Items);
  AutoComplete := False;
  FStoredItems.OnChange := StoredItemsChange;
  doFilter := True;
  StoredItemIndex := -1;
end;

這是我嘗試調用但返回錯誤消息:

procedure TfrmMain.FormActivate(Sender: TObject);
begin
  TNewComponent.InitCombo;
end;

Error Messages
[dcc32 Error] makeasale_u_v1.pas(84): E2076 This form of method call only allowed for class methods or constructor

請注意,編譯、構建、安裝進展順利並且正在運行。 除了如何調用組件內部的過程?

僅基於您問題第一段的以下部分

有一個過程 InitCombo 需要在實現中調用。

看來您至少對編寫組件的一些事情感到困惑。

首先,您可以在其構造函數中初始化組件屬性以初始化事物,或者在重寫的Loaded方法中初始化,該方法在組件從使用該組件的 .dfm 文件流入后調用。 請注意, Loaded的更改不應觸及用戶可以在 Object 檢查器中設置的屬性或事件,因為這樣做會阻止使用用戶的設置。

constructor TNewComponent.Create(AOwner: TComponent);
begin
  inherited;
  // Do your stuff here
end;

procedure TNewComponent.Loaded;
begin
  // Do your stuff here
end;

其次,發布的事件(可以在 Object 檢查器的事件選項卡中看到)屬於使用組件的程序員,而不是組件作者。 永遠不要對那些事件處理程序做任何事情。 如果最終用戶分配了處理程序,您的組件不應該接觸這些事件,除非調用它們。 所以下面的代碼是絕對不正確的,因為OnChange事件屬於你組件的用戶,而不是你的組件代碼:

procedure TNewComponent.InitCombo;       //TComboBox ancestor
begin
  FStoredItems.OnChange := nil;
  ...
  FStoredItems.OnChange := StoredItemsChange;
end;

如果您絕對必須這樣做,則需要通過保存最終用戶分配的任何事件處理程序來正確執行此操作,然后再將其恢復:

procedure TNewComponent.InitCombo;
var
  OldOnChange: TNotifyEvent;
begin
  OldOnChange := Self.OnChange;
  // Do your stuff here
  Self.OnChange := OldOnChange;
end;

Third, unless you're using a class procedure or class function , you cannot call a method on a class itself (in other words, you cannot use TNewComponent.DoSomething ). 您在組件本身的實例上調用方法或訪問屬性。 在您的組件代碼中,這將通過使用Self來完成,它引用當前實現的組件,如Self.DoSomething

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM