請問一個TCollection的問題 |
缺席
|
iamjsn
初階會員 發表:78 回覆:95 積分:44 註冊:2002-08-16 發送簡訊給我 |
我使用TCollection寫了一個元件,大概如下
元件裝來來之後預設的TCollection屬性編輯器一切運作正常
但是在設定TBtn的onClick 事件時,就是當我在event那頁,對OnClick按兩下要產生事件時,卻出現了一個錯誤訊息
"Can't Create a Method for a unnamed component"
我本來以為是一定要元件才能產生method,後來今天在william介紹的網站上看到一組免費的元件http://www.droopyeyes.com/default.asp?mode=ShowProduct&ID=3,我下載按裝後發現其中的dibIMageList的寫法架構跟我一樣也是用CollectionItem, 但它的event卻能順利產生。
它的itme在按兩下要產生事件時會自動產生一個prodecure 名稱,我的不會。不知是那裏少寫了。
TBtn = class(TCollectionItem) published property OnClick: TNotifyEvent read FOnClick write FOnClick stored IsOnClickStored; end; TBtns = class(TCollection) end; THccBtnCtrl = class(TComponent) published property Items: TBtns read FItems write FItems; end;下面是完整的source ////////////////////////////////////////////////////// TBtn = class(TCollectionItem) private // FDisplayName: string; FImportedFrom: string; FCaption: string; FImageIndexUp: Integer; FImageIndexDown: Integer; FImageIndexOver: Integer; FOnClick: TNotifyEvent; FDrawRect: TRect; FLastState: TLastState; FHint: String; FTabColor: TColor; FIconRect: TRect; FCaptionRect: TRect; FPaintMode: TPaintMode; FPaintState: TPaintState; FDown: boolean; FOnTranslateImagePath: TDIBTranslateImagePathEvent; procedure SetCaption(const Value: string); procedure SetImageIndexUp(const Value: Integer); function IsOnClickStored: Boolean; procedure SetLastState(const Value: TLastState); procedure SetTabColor(const Value: TColor); procedure ReadTemplateFilename(Reader: TReader); procedure WriteTemplateFilename(Writer: TWriter); public procedure DefineProperties(Filer: TFiler); override; constructor Create(Collection: TCollection); override; property LastState:TLastState read FLastState write SetLastState; Property PaintMode:TPaintMode read FPaintMode write FPaintMode; Property PaintState:TPaintState read FPaintState write FPaintState; published property DisplayName; property Index; property Caption: string read FCaption write SetCaption; property ImageIndexUp:Integer read FImageIndexUp write SetImageIndexUp; property ImageIndexOver:Integer read FImageIndexOver write FImageIndexOver; property ImageIndexDown:Integer read FImageIndexDown write FImageIndexDown; property OnClick: TNotifyEvent read FOnClick write FOnClick stored IsOnClickStored; property DrawRect:TRect read FDrawRect write FDrawRect; property IconRect:TRect read FIconRect write FIconRect; property CaptionRect:TRect read FCaptionRect write FCaptionRect; property Hint:String read FHint write FHint; property TabColor:TColor read FTabColor write SetTabColor; property Down:boolean read FDown write FDown; end; TBtns = class(TCollection) private FOwner: THccBtnCtrl; function GetItem(Index: Integer): TBtn; procedure SetItem(Index: Integer; const Value: TBtn); protected public constructor Create(AOwner: THccBtnCtrl); function Add: TBtn; procedure Update(Item: TCollectionItem); override; Property Owner: THccBtnCtrl Read FOwner; function AddTemplate(const GUID: string; const Index: Integer): TBtn; //--關於陣列屬性default的說明 -- //The definition of an array property can be followed by the default directive, // in which case the array property becomes the default property of the class. For example, // //type // TStringArray = class // public // property Strings[Index: Integer]: string ...; default; // ... // end; // //If a class has a default property, //you can access that property with the abbreviation object[index], //which is equivalent to object.property[index]. property Btns[Index: Integer]:TBtn read GetItem write SetItem;default; end; -------------------------------------------------------------------------- { TBtn } constructor TBtn.Create(Collection: TCollection); begin inherited Create(Collection); // FDisplayName := inherited GetDisplayName; FLastState:=lsMouseUp; ImageIndexUp:=index; ImageIndexOver:=-1; ImageIndexDown:=-1; FTabColor:=clWhite; caption:='檔案管理'; FDown:=false; end; procedure TBtn.DefineProperties(Filer: TFiler); begin inherited; Filer.DefineProperty('TemplateFilename', ReadTemplateFilename, WriteTemplateFilename, (FImportedFrom <> '')); end; function TBtn.IsOnClickStored: Boolean; begin end; procedure TBtn.ReadTemplateFilename(Reader: TReader); begin FImportedFrom := Reader.ReadString; end; procedure TBtn.SetCaption(const Value: string); begin FCaption := Value; end; procedure TBtn.SetImageIndexUp(const Value: Integer); begin FImageIndexUp := Value; end; procedure TBtn.SetLastState(const Value: TLastState); begin FLastState := Value; end; procedure TBtn.SetTabColor(const Value: TColor); begin FTabColor := Value; end; procedure TBtn.WriteTemplateFilename(Writer: TWriter); begin Writer.WriteString(FImportedFrom); end; { TBtns } function TBtns.Add: TBtn; begin Result := TBtn(inherited Add); end; function TBtns.AddTemplate(const GUID: string; const Index: Integer): TBtn; var I, Position: Integer; SearchString: string; begin Position := -1; SearchString := GUID ':' IntToStr(Index); for I := 0 to Count - 1 do begin if Btns[I].FImportedFrom = SearchString then begin Position := I; break; end; end; if Position > -1 then Result := Btns[Position] else begin Result := Add; Result.FImportedFrom := GUID ':' IntToStr(Index); end; end; constructor TBtns.Create(AOwner: THccBtnCtrl); begin inherited Create(TBtn); FOwner := AOwner; end; function TBtns.GetItem(Index: Integer): TBtn; begin Result:=TBtn(Items[index]); end; procedure TBtns.SetItem(Index: Integer; const Value: TBtn); begin Items[index]:= Value; end; procedure TBtns.Update(Item: TCollectionItem); begin inherited; Owner.DoChange; end; |
iamjsn
初階會員 發表:78 回覆:95 積分:44 註冊:2002-08-16 發送簡訊給我 |
忙了好幾天終於.....找到原因了,雖然一時還搞不清楚為什麼。但是能運作了 就是collectionitem要產生事件的話 不能繼承TCollection, 而要繼承TOwnedCollection
即
TBtns = class(TOwnedCollection)
在TOwnedCollection類別上面有如下的註解:
{ Collection class that maintains an "Owner" in order to obtain property
path information at design-time } 讓大家參考一下
|
iamjsn
初階會員 發表:78 回覆:95 積分:44 註冊:2002-08-16 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |