VCL类学习之(六) TCollectionItem

TCollectionItem represents an item in a collection.
Unit
Classes
Description
A TCollection holds a group of TCollectionItem objects. TCollectionItems are created and destroyed by TCollection抯 Add and Clear methods. Each TCollectionItem has a Collection property that points to the TCollection object to which the item belongs.
Objects descended from TCollection can contain objects descended from TCollectionItem. For example, a TDBGridColumns object contains TColumn objects; these two classes are used by TDBGrid to represent grid columns.
  1.  TCollectionItem = class(TPersistent)
  2.   private
  3.     FCollection: TCollection;
  4.     FID: Integer;
  5.     function GetIndex: Integer;
  6.   protected
  7.     procedure Changed(AllItems: Boolean);
  8.     function GetOwner: TPersistent; override;
  9.     function GetDisplayName: string; virtual;
  10.     procedure SetCollection(Value: TCollection); virtual;
  11.     procedure SetIndex(Value: Integer); virtual;
  12.     procedure SetDisplayName(const Value: string); virtual;
  13.   public
  14.     constructor Create(Collection: TCollection); virtual;
  15.     destructor Destroy; override;
  16.     function GetNamePath: string; override;
  17.     property Collection: TCollection read FCollection write SetCollection;
  18.     property ID: Integer read FID;
  19.     property Index: Integer read GetIndex write SetIndex;
  20.     property DisplayName: string read GetDisplayName write SetDisplayName;
  21.   end;
  22. { TCollectionItem }
  23. constructor TCollectionItem.Create(Collection: TCollection);
  24. begin
  25.   SetCollection(Collection);
  26. end;
  27. destructor TCollectionItem.Destroy;
  28. begin
  29.   SetCollection(nil);
  30.   inherited Destroy;
  31. end;
  32. procedure TCollectionItem.Changed(AllItems: Boolean);
  33. var
  34.   Item: TCollectionItem;
  35. begin
  36.   if (FCollection <> niland (FCollection.FUpdateCount = 0then
  37.   begin
  38.     if AllItems then Item := nil else Item := Self;
  39.     FCollection.Update(Item);
  40.   end;
  41. end;
  42. function TCollectionItem.GetIndex: Integer;
  43. begin
  44.   if FCollection <> nil then
  45.     Result := FCollection.FItems.IndexOf(Self) else
  46.     Result := -1;
  47. end;
  48. function TCollectionItem.GetDisplayName: string;
  49. begin
  50.   Result := ClassName;
  51. end;
  52. function TCollectionItem.GetNamePath: string;
  53. begin
  54.   if FCollection <> nil then
  55.     Result := Format('%s[%d]',[FCollection.GetNamePath, Index])
  56.   else
  57.     Result := ClassName;
  58. end;
  59. function TCollectionItem.GetOwner: TPersistent;
  60. begin
  61.   Result := FCollection;
  62. end;
  63. procedure TCollectionItem.SetCollection(Value: TCollection);
  64. begin
  65.   if FCollection <> Value then
  66.   begin
  67.     if FCollection <> nil then FCollection.RemoveItem(Self);
  68.     if Value <> nil then Value.InsertItem(Self);
  69.   end;
  70. end;
  71. procedure TCollectionItem.SetDisplayName(const Value: string);
  72. begin
  73.   Changed(False);
  74. end;
  75. procedure TCollectionItem.SetIndex(Value: Integer);
  76. var
  77.   CurIndex: Integer;
  78. begin
  79.   CurIndex := GetIndex;
  80.   if (CurIndex >= 0and (CurIndex <> Value) then
  81.   begin
  82.     FCollection.FItems.Move(CurIndex, Value);
  83.     Changed(True);
  84.   end;
  85. end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值