Re: TStringGrid



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

.



Relevant Pages

  • Re: TStringGrid
    ... Save the RowCount property of the TStringGrid to a file followed by the Text strings of each Rowselement. ... StrLen: integer; begin {get length of string} StrLen:= Length; ... i: integer; RowText: string; begin ...
    (alt.comp.lang.borland-delphi)
  • TStringGrid question
    ... The help text for TStringGrid says; ... ability to associate an object with each string in the grid. ... I interpret this to mean that grid array elements can be associated ...
    (alt.comp.lang.borland-delphi)
  • Re: TStringGrid
    ... zero length string and so the code in my utility file ia now ... ... {write characters} ... LenStr:= ReadStreamInt; ...
    (alt.comp.lang.borland-delphi)
  • Re: StringGrid FontColor
    ... SG:= Sender as tStringGrid; ... // choose the cells you wish to colour red ... // Write your string ...
    (alt.comp.lang.borland-delphi)
  • Re: Calculate the string statement
    ... verkn:(vkn:char; ... funkt:(fkt:string; ... var fkt,dfkt: p; ... var fehler: boolean; ...
    (comp.lang.pascal.borland)