Attached is a copy of my TStringCollection component which I use for this
list of stringlists.
I use it like:
StringCollection['name of stringlist'].Lines.Strings[1];
StringCollection.StringDefs.Add.Lines.LoadFromFile('somefile');
Mark
---cut here---
unit StringCollection;
(* $Id: StringCollection.pas,v 1.12 2000/10/24 07:13:47 derricuttm Exp $
*
* TStringCollection is something I've been wanting to make up for ages but
* never got there. It basically extends the TStringThing component to have
* multple stringlists and keeping things nice and tidy without cluttering
up
* the form.
*
* You can access the strings in code with:
*
* StringCollection.Strings['Project'].ShowOkMessage;
* StringCollection['Another String'].ShowCustomMessage;
*
* Mark Derricutt <[EMAIL PROTECTED]>
*
*)
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db
{,ColnEdit};
type
TStringDef = class;
TStringDefs = class(TOwnedCollection)
public
constructor Create(AOwner: TPersistent);
function AddStringDef: TStringDef;
end;
TStringDef = class(TNamedItem)
private
fLines: TStrings;
fContext: Integer;
fButtons: TMsgDlgButtons;
procedure SetLines(const Value: TStrings);
function GetText: string;
function GetValues(ItemName: string): string;
procedure SetValues(ItemName: string; const Value: string);
public
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
procedure ShowOkMessage;
function ShowYesNoMessage: Word;
function ShowCustomMessage: Word;
property Text: string read GetText;
property Values[ItemName: string]: string read GetValues write
SetValues;
published
property Lines: TStrings read fLines write SetLines;
property Buttons: TMsgDlgButtons read fButtons write fButtons;
property HelpContext: Integer read fContext write fContext;
end;
TStringCollection = class(TComponent)
private
fStringDefs: TStringDefs;
function GetStrings: TStringDefs;
procedure SetStrings(const Value: TStringDefs);
function GetStringByName(StringName: string): TStringDef;
{ Private declarations }
protected
{ Protected declarations }
public
function HasStrings: Boolean;
function StringIndex(StringName: string): Integer;
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
property Strings[StringName: string]: TStringDef read GetStringByName;
default;
{ Public declarations }
published
property StringDefs: TStringDefs read GetStrings write SetStrings
stored
HasStrings;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Additional', [TStringCollection]);
end;
{ TStringCollection }
constructor TStringCollection.Create(Owner: TComponent);
begin
inherited;
fStringDefs := TStringDefs.Create(Self);
end;
destructor TStringCollection.Destroy;
begin
fStringDefs.Free;
inherited;
end;
function TStringDef.GetValues(ItemName: string): string;
begin
Result := Lines.Values[ItemName];
end;
procedure TStringDef.SetValues(ItemName: string; const Value: string);
begin
Lines.Values[ItemName] := Value;
end;
function TStringCollection.GetStrings: TStringDefs;
begin
if fStringDefs = nil then
fStringDefs := TStringDefs.Create(Self);
Result := fStringDefs;
end;
(* Summary:
* Returns true if component has any TStringDef's defined.
* Return Value:
* TRUE if there are any TStringDef's defined,
* FALSE if empty.
* Description:
* The HasStrings function is used internally by the StringDef's
property to
* determine if it should be saved into the unit's DFM file. However
it can
* also be used by the developer if they so like.
*)
function TStringCollection.HasStrings: Boolean;
begin
Result := (FStringDefs <> nil) and (FStringDefs.Count > 0);
end;
procedure TStringCollection.SetStrings(const Value: TStringDefs);
begin
StringDefs.Assign(Value);
end;
{ TStringsDef }
function TStringDefs.AddStringDef: TStringDef;
begin
Result := TStringDef.Create(Self);
end;
constructor TStringDefs.Create(AOwner: TPersistent);
begin
inherited Create(AOwner, TStringDef);
end;
(* Summary:
* Return a TStringDef object by referring to its name.
* Arguments:
* StringName - The name property of the TStringDef object.
* Return Value:
* NIL is invalid TStringDef reference,
* TStringDef if valid.
* Description:
* The GetStringByName function is the read method of the Strings
property,
* it is used to access a member of the StringDef's collection. The
Strings
* property is also the default property for a TStringCollection
component so
* you can reference it via StringCollection1['SomeStringDefName'].Text;
*)
function TStringCollection.GetStringByName(StringName: string): TStringDef;
var
i: integer;
begin
i := StringIndex(StringName);
if i > -1 then
Result := TStringDef(StringDefs.Items[i])
else
raise Exception.CreateFmt(
'"%s" is an unknown String Definition for %s.%s',
[StringName, Owner.Name, Name]);
{
for i := 0 to StringDefs.Count - 1 do
begin
if TStringDef(StringDefs.Items[i]).Name = StringName then
begin
Result := TStringDef(StringDefs.Items[i]);
Exit;
end;
end;
raise Exception.CreateFmt(
'"%s" is an unknown String Definition for %s.%s',
[StringName, Owner.Name, Name]);
}
end;
function TStringCollection.StringIndex(StringName: string): Integer;
var
i: integer;
begin
Result := -1;
for i := 0 to StringDefs.Count - 1 do
begin
if TStringDef(StringDefs.Items[i]).Name = StringName then
begin
Result := i;
Exit;
end;
end;
end;
{ TStringDef }
constructor TStringDef.Create(Collection: TCollection);
begin
inherited;
fLines := TStringList.Create;
end;
destructor TStringDef.Destroy;
begin
fLines.Free;
inherited;
end;
procedure TStringDef.SetLines(const Value: TStrings);
begin
fLines.Assign(Value);
end;
procedure TStringDef.ShowOkMessage;
begin
MessageDlg(Lines.Text,
mtInformation,
[mbOk],
HelpContext);
end;
function TStringDef.ShowYesNoMessage: Word;
begin
Result := MessageDlg(Lines.Text,
mtConfirmation,
[mbYes, mbNo],
HelpContext);
end;
function TStringDef.ShowCustomMessage: Word;
begin
Result := MessageDlg(Lines.Text,
mtConfirmation,
Buttons,
HelpContext);
end;
function TStringDef.GetText: string;
begin
Result := Lines.Text;
end;
end.
---cut here---
--On Friday, 4 May 2001 12:21 p.m. +1200 [EMAIL PROTECTED] wrote:
> A possible ... A TList of TStringLists ...
>
> Regards
> Paul McKenzie
>
> =========================
> Paul McKenzie
> Jetbet II Developer
> =========================
> [EMAIL PROTECTED]
> Ph: (04) 576-6822
>
> T.A.B. National Office
> 106-110 Jackson Street
> Petone
> New Zealand
>
>
>
> ____________________Reply Separator____________________
> Subject: Re: [DUG]: Accessing 'Fields' in a String List
> Author: [EMAIL PROTECTED]
> Date: 04/05/2001 12:18
>
>
>
> In this case we usually create a tempory stringlist and assign one
> line of the source list to it
>
> i.e
>
> TempList.Text := SourceList.CommaText;
>
> In your case this leaves templist with 5 items which you can access
> as normal, eg TempList[3] would be the third comma seperated item in
> SourceList.
>
> Hope this helps.
>
>
> Rob
> Software engineer
> Wild Software Ltd
> [EMAIL PROTECTED]
> ----- Original Message -----
> From: Mark Howard
> To: Multiple recipients of list delphi
> Sent: Saturday, May 05, 2001 6:15 AM
> Subject: [DUG]: Accessing 'Fields' in a String List
>
>
> Hi
>
> I have a TStringList in which each of the strings is itself a comma
> delimited list of 5 fields.
>
> Is there a TStrings method that lets me access these fields
> directly, or do I have to chop the string myself?
>
> TIA
>
> Mark(See attached file: att1.htm)
> (See attached file: att2.eml)
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"