Mega Code Archive

 
Categories / Delphi / Strings
 

String Store Manipulate Multipuple TStrings

Title: String Store Manipulate Multipuple TStrings Question: TStringStore is a TComponent to manipulate multiple TStrings Answer: unit StringStore; {*******************************************************} { } { GT Delphi Components } { StringStore } { } { Copyright (c) GT Delphi Components } { http://www.gtdelphicomponents.gr } { } { } {*******************************************************} interface uses Classes ,Contnrs ; type {------------------------------------------------------------------------------} TStoreStringList = class(TStringList) private FGroupID: Integer; FName : string; { Private declarations } protected { Protected declarations } public { Public declarations } property Name : string read FName write FName; property GroupID : Integer read FGroupID write FGroupID; end; {------------------------------------------------------------------------------} TStringStore = class(TComponent) private { Private declarations } FStringObjectList : TObjectList; function GetStringStoreList(Index: Integer): TStoreStringList; function GetStringStoreListByName(Name: string): TStoreStringList; protected { Protected declarations } function GetStringStoreListIndex(Name : string):Integer; public { Public declarations } constructor Create(AOwner : TComponent);override; destructor Destroy;override; public procedure AddNew(Name : string;GroupId:Integer = -1);overload; procedure AddNew(StoreStringList : TStoreStringList);overload; procedure Delete(Index : Integer);overload; procedure Delete(Name : string);overload; procedure Clear; public property Items [Index: Integer]: TStoreStringList read GetStringStoreList;default; property ItemByName[Name : string] : TStoreStringList read GetStringStoreListByName; published { Published declarations} end; {------------------------------------------------------------------------------} implementation uses SysUtils ; { TStringStore } {------------------------------------------------------------------------------} constructor TStringStore.Create(AOwner: TComponent); begin inherited Create(AOwner); FStringObjectList := TObjectList.Create; FStringObjectList.OwnsObjects := True; end; {------------------------------------------------------------------------------} destructor TStringStore.Destroy; begin FStringObjectList.Free; inherited; end; {------------------------------------------------------------------------------} function TStringStore.GetStringStoreListIndex(Name: string): Integer; var i : integer; begin Result := -1; for i:= 0 to Pred(FStringObjectList.Count) do begin if SameText(Items[i].Name,Name) then begin Result := i; Break; end; end; end; {------------------------------------------------------------------------------} procedure TStringStore.AddNew(Name: string;GroupId:Integer = -1); var STList : TStoreStringList; begin STList := TStoreStringList.Create; STList.Name := Name; STList.GroupID := GroupId; FStringObjectList.Add(STList); end; {------------------------------------------------------------------------------} procedure TStringStore.AddNew(StoreStringList: TStoreStringList); begin if Assigned(StoreStringList) then begin FStringObjectList.Add(StoreStringList); end; end; {------------------------------------------------------------------------------} procedure TStringStore.Clear; begin FStringObjectList.Clear; end; {------------------------------------------------------------------------------} procedure TStringStore.Delete(Index: Integer); begin FStringObjectList.Delete(Index); end; {------------------------------------------------------------------------------} procedure TStringStore.Delete(Name: string); begin FStringObjectList.Delete(GetStringStoreListIndex(Name)); end; {------------------------------------------------------------------------------} function TStringStore.GetStringStoreList(Index: Integer): TStoreStringList; begin Result := TStoreStringList(FStringObjectList.Items[Index]); end; {------------------------------------------------------------------------------} function TStringStore.GetStringStoreListByName(Name: string): TStoreStringList; begin Result := TStoreStringList(FStringObjectList.Items[GetStringStoreListIndex(Name)]); end; {------------------------------------------------------------------------------} end.