TCollection 如何得知清單(Item)數量Count? |
尚未結案
|
jeffreck
高階會員 發表:247 回覆:340 積分:197 註冊:2003-01-23 發送簡訊給我 |
請教各前輩 TCollection 如何得知清單(Item)數量Count? 我寫的 VCL 如下,但Count 永遠都是1,不知是那錯了?? 謝謝各位前輩!!
------------- TStyleItems = class(TCollection) private FOwner: TComponent; function GetItem(Index: Integer): TStyleItem; procedure SetItem(Index: Integer; const Value: TStyleItem); protected function GetOwner: TPersistent; override; public constructor Create(AOwner: TComponent); function Add: TStyleItem; property Items[Index: Integer]: TStyleItem read GetItem write SetItem; default; function StyleByName(FindStyleName : String ):Integer; end; implementation { TSimpleItems } function TStyleItems.Add: TStyleItem; begin Result := TStyleItem(inherited Add); end; constructor TStyleItems.Create(AOwner: TComponent); begin inherited Create(TStyleItem); FOwner := AOwner; end; function TStyleItems.GetItem(Index: Integer): TStyleItem; begin Result := TStyleItem(inherited GetItem(Index)); end; function TStyleItems.GetOwner: TPersistent; begin Result := FOwner; end; procedure TStyleItems.SetItem(Index: Integer; const Value: TStyleItem); begin inherited SetItem(Index, Value); end; function TStyleItems.StyleByName(FindStyleName : String ):Integer; Var I : Integer; Begin Result:=-1;For I:=0 to Count-1 do //Count 都是1,不會變?? Begin if GetItem(I).FStyleName=FindStyleName then Begin Result:=I; exit; end; end; end; |
Zard
尊榮會員 發表:24 回覆:396 積分:539 註冊:2003-11-26 發送簡訊給我 |
引言: 請教各前輩 TCollection 如何得知清單(Item)數量Count? 我寫的 VCL 如下,但Count 永遠都是1,不知是那錯了?? 謝謝各位前輩!!您所貼的碼只有部份, 可否放上完整的碼比較好幫你? 我剛才看了一下TCollection的碼, 他的實作方式其實是把TCollectionItem 一個個加入一個TList(FItems)內, 其成員Count, 就是把FItems內的Count回 傳給你, 但是什麼時候才會加入FItems內呢? 是在呼叫TCollection.InsertItem 時才會加入, 只有呼叫TCollection.Add似乎是不行的, 我把相關的碼貼上來:------------- TStyleItems = class(TCollection) private FOwner: TComponent; function GetItem(Index: Integer): TStyleItem; procedure SetItem(Index: Integer; const Value: TStyleItem); protected function GetOwner: TPersistent; override; public constructor Create(AOwner: TComponent); function Add: TStyleItem; property Items[Index: Integer]: TStyleItem read GetItem write SetItem; default; function StyleByName(FindStyleName : String ):Integer; end; implementation { TSimpleItems } function TStyleItems.Add: TStyleItem; begin Result := TStyleItem(inherited Add); end; constructor TStyleItems.Create(AOwner: TComponent); begin inherited Create(TStyleItem); FOwner := AOwner; end; function TStyleItems.GetItem(Index: Integer): TStyleItem; begin Result := TStyleItem(inherited GetItem(Index)); end; function TStyleItems.GetOwner: TPersistent; begin Result := FOwner; end; procedure TStyleItems.SetItem(Index: Integer; const Value: TStyleItem); begin inherited SetItem(Index, Value); end; function TStyleItems.StyleByName(FindStyleName : String ):Integer; Var I : Integer; Begin Result:=-1;For I:=0 to Count-1 do //Count 都是1,不會變??Begin if GetItem(I).FStyleName=FindStyleName then Begin Result:=I; exit; end; end; end; TCollection = class(TPersistent) private ...... FItems: TList; function GetCount: Integer; procedure InsertItem(Item: TCollectionItem); ...... public ...... function Add: TCollectionItem; property Count: Integer read GetCount; ....... end; procedure TCollection.InsertItem(Item: TCollectionItem); begin ....... FItems.Add(Item); ....... end; function TCollection.GetCount: Integer; begin Result := FItems.Count; end; function TCollection.Add: TCollectionItem; begin Result := FItemClass.Create(Self); end;這是我的猜測, 如果不對的話可能要請你放上更完整的碼才能幫你了 |
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
|
jeffreck
高階會員 發表:247 回覆:340 積分:197 註冊:2003-01-23 發送簡訊給我 |
謝謝前輩,以下為程式碼共兩個檔案..
unit JFStyleItem; interface uses Windows, Messages, SysUtils,Graphics, Classes; Type {// TJFStyle} TJFDBForm_Style =class(TPersistent) // class(TPersistent) private { Private declarations } FViewColor:TColor; FEditColor:Tcolor; FAddNewColor:Tcolor; protected { Protected declarations } public { Public declarations } //不做試試 procedure Assign(Source: TPersistent);Override; published { Published declarations } Property ViewColor : TColor Read FViewColor Write FViewColor ; Property AddNewColor : TColor Read FAddNewColor Write FAddNewColor ; Property EditColor : TColor Read FEditColor Write FEditColor ; end; // {StyleItem } TStyleItem =class(TCollectionItem) // class(TPersistent) private { Private declarations } FJFStyle : TJFDBForm_Style ; FStyleName:String; FTag:String; protected { Protected declarations } public { Public declarations } constructor Create(Collection: TCollection); override; // constructor Create(Aowner:TComponent);override; destructor Destroy;override; // procedure Assign(Source: TPersistent);Override; published { Published declarations } Property StyleName : String Read FStyleName Write FStyleName; Property Tag: String Read FTag Write FTag; Property JFStyle: TJFDBForm_Style Read FJFStyle Write FJFStyle; end; // TStyleItems = class(TCollection) private FOwner: TComponent; function GetItem(Index: Integer): TStyleItem; procedure SetItem(Index: Integer; const Value: TStyleItem); protected function GetOwner: TPersistent; override; public constructor Create(AOwner: TComponent); function Add: TStyleItem; property Items[Index: Integer]: TStyleItem read GetItem write SetItem; default; function StyleByName(FindStyleName : String ):Integer; end; implementation {StyleItem} constructor TStyleItem.Create(Collection: TCollection); begin inherited Create(Collection); FJFStyle :=TJFDBForm_Style.Create; FJFStyle.ViewColor := $00CDFFB5; FJFStyle.AddNewColor := $00C1FFFE; FJFStyle.EditColor := $00C1FFFE; end; destructor TStyleItem.Destroy; begin inherited Destroy; end; { TSimpleItems } function TStyleItems.Add: TStyleItem; begin Result := TStyleItem(inherited Add); end; constructor TStyleItems.Create(AOwner: TComponent); begin inherited Create(TStyleItem); FOwner := AOwner; end; function TStyleItems.GetItem(Index: Integer): TStyleItem; begin Result := TStyleItem(inherited GetItem(Index)); end; function TStyleItems.GetOwner: TPersistent; begin Result := FOwner; end; procedure TStyleItems.SetItem(Index: Integer; const Value: TStyleItem); begin inherited SetItem(Index, Value); end; function TStyleItems.StyleByName(FindStyleName : String ):Integer; Var I : Integer; Begin Result:=-1; For I:=0 to Count-1 do Begin if GetItem(I).FStyleName=FindStyleName then Begin Result:=I; exit; end; end; end; end. ----------------------------------------- unit Test; interface uses SysUtils, Classes,JFStyleItem; type TTest = class(TComponent) private { Private declarations } FStyleItems :TStyleItems ; protected { Protected declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure SetStyleItems(const Value: TStyleItems); public { Public declarations } published { Published declarations } Property StyleItems : TStyleItems Read FStyleItems Write SetStyleItems; end; procedure Register; implementation constructor TTest.Create(AOwner: TComponent); begin inherited Create(AOwner); FStyleItems := TStyleItems.Create(Self); end; destructor TTest.Destroy; begin FStyleItems.Free; inherited Destroy; end; procedure TTest.SetStyleItems(const Value: TStyleItems); begin StyleItems.Assign(Value); end; procedure Register; begin RegisterComponents('JFControls', [TTest]); end; end. |
Zard
尊榮會員 發表:24 回覆:396 積分:539 註冊:2003-11-26 發送簡訊給我 |
可是我試可以啊, Count值都正確的, 我把我測試的碼貼給你參考
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, JFStyleItem, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; FStyleItems, FStyleItems2 :TStyleItems ; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); begin FStyleItems := TStyleItems.Create(Self); FStyleItems2:= TStyleItems.Create(Self); end; procedure TForm1.FormDestroy(Sender: TObject); begin FStyleItems.Free; FStyleItems2.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin FStyleItems.Add; // 顯示Add後的Count值 ShowMessage(IntToStr(FStyleItems.Count)); end; procedure TForm1.Button2Click(Sender: TObject); begin //在這設中斷點, 進去看你在StyleByName()裡的Count也是正確的 FStyleItems.StyleByName('123'); end; procedure TForm1.Button3Click(Sender: TObject); begin // 把目前FStyleItems Assign 為 FStyleItems2 // 再試上方的兩個按鈕, 值也是正確的. FStyleItems.Assign(FStyleItems2); end; end. |
jeffreck
高階會員 發表:247 回覆:340 積分:197 註冊:2003-01-23 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |