Re: TStringGrid
- From: alanglloyd@xxxxxxx
- Date: 9 Apr 2005 23:03:37 -0700
TStringGrid has a Rows property which is an array each element of which
is a TStringList. Each TStringList has a Text property which is a cRLF
delimited list of the strings. Save the RowCount property of the
TStringGrid to a file followed by the Text strings of each Rows[]
element. As follows ...
procedure WriteStreamStr(Stream : TStream; Str : string);
{writes a string to the stream}
var
StrLen : integer;
begin
{get length of string}
StrLen := Length(Str);
{write length of string}
Stream.WriteBuffer(StrLen, SizeOf(Integer));
{write characters}
Stream.Write(Str[1], StrLen);
end;
function ReadStreamStr(Stream : TStream) : string;
{returns a string from the stream}
var
LenStr : integer;
begin
Result := '';
{get length of string}
Stream.ReadBuffer(LenStr, SizeOf(Integer));
{set string to get memory}
SetLength(Result, LenStr);
{read characters}
Stream.Read(Result[1], LenStr);
end;
procedure SGSaveToFile(SG : TStringGrid; FPN : string);
var
FS : TFileStream;
i : integer;
begin
FS := TFileStream.Create(FPN, fmCreate);
try
{store row count}
FS.Write(SG.RowCount, SizeOf(integer));
{store text of each row stringlist}
for i := 0 to SG.RowCount - 1 do
WriteStreamStr(FS, SG.Rows[i].Text);
finally
FS.Free;
end;
end;
procedure SGLoadFromFile(SG : TStringGrid; FPN : string);
var
FS : TFileStream;
SGRowCount, i : integer;
RowText : string;
begin
FS := TFileStream.Create(FPN, fmOpenRead);
try
{read & set count of rows}
FS.Read(SGRowCount, SizeOf(integer));
SG.RowCount := SGRowCount;
{read & set each row}
for i := 0 to SGRowCount - 1 do begin
RowText := ReadStreamStr(FS);
SG.Rows[i].Text := RowText;
end;
finally
FS.Free;
end;
end;
Learn a bit more about a TStringGrid by browsing through the Delphi
help file and the properties of its various objects.
Alan Lloyd
.
- Follow-Ups:
- Re: TStringGrid
- From: J French
- Re: TStringGrid
- From: Anthony
- Re: TStringGrid
- References:
- TStringGrid
- From: Anthony
- TStringGrid
- Prev by Date: TStringGrid
- Next by Date: Rich Edit and Symbol Fonts
- Previous by thread: TStringGrid
- Next by thread: Re: TStringGrid
- Index(es):
Relevant Pages
|