How to Save and load data to a text file

Hi

I need to save the contents of a stringgrid and a listbox all to the same text file, using savedialog, then load the settings back using open dialog.

How can I do this?

Thanks
Asw
LVL 1
AswAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rfedykCommented:
Hi ASW

TStringgrid does not have a convenient feature for saving cell contents. You have to scroll through each Cell contents and build a a string that can be written to a file. If you know that the contents of a Cell do not contain a TAB character (#09) you can use that character as a separator when you parse your text file from the OpenDialog.


Some thing like this will work

function  StringAfter(Delim,AString:string):string;
begin
 if pos(Delim,AString)=0 then
  Result:=AString
 else
  Result:=copy(AString,pos(Delim,AString)+length(Delim),length(AString));
end;


function StringBefore(Delim,AString:string):string;
begin
 if pos(Delim,AString)=0 then
  Result:=AString
 else
  Result:=copy(AString,1,pos(Delim,AString)-1);
end;

procedure SaveStringGrid;
var r,c:integer;
     SL:TStringList;
    TmpS:string;
begin
 SL:=TStringList.Create;
 SL.Add(IntToStr(StringGrid1.RowCount)); //we need to know later how many rows there are in the stringgrid
 for r:=0 to StringGrid1.RowCount-1 do
  begin
   TmpS:='';
   for c:=0 to StringGrid1.ColCount-1 do
    begin
     TmpS:=TmpS+StringGrid1.Cells[c,r]+#09;  
    end;
   SetLength(TmpS,length(TmpS)-1);  //remove the last TAB character
   SL.Add(TmpS);
  end;
 SL.AddStrings(Listbox1.Items); //add the list box strings
 SL.SaveToFile(SaveDialog1.FileName);
 SL.Free;
end;

procedure PopulateStringGrid;
var r,c,cl,clp,rl:integer;
     SL:TStringList;
    TmpS,TS:string;
begin
 SL:=TStringList.Create;
 SL.LoadFromFile(OpenDialog1.FileName);
 rl:=StrToInt(SL[0]);  //get the number of rows for the stringgrid
 //first we need to see how many columns we need
 TmpS:=SL[1];
 cl:=1;  //we start with 1 because the last Cell string is not delimited by #9
 for clp:=1 to length(TmpS) do
  if TmpS[clp]=#9 then
   inc(cl);

 StringGrid1.ColCount:=cl;
 StringGrid1.RowCount:=rl;
 for r:=0 to StringGrid1.RowCount-1 do
  begin
   TmpS:=SL[r+1]; //we need the +1 to step over the first string
   for c:=0 to StringGrid1.ColCount-1 do
    begin
     if c<>(StringGrid1.ColCount-1) then
      begin
       StringGrid1.Cells[c,r]:=StringBefore(#9,TmpS);
       TmpS:=StringAfter(#9,TmpS);  
      end
     else
       StringGrid1.Cells[c,r]:=TmpS;
    end;
  end;
 SL.Free;
 //now we load the list box
 ListBox1.Items.LoadFromFile(OpenDialog1.FileName);
 for r:=0 to rl do
  ListBox1.Items.Delete(r); //get rid of the strings that belong to the string grid
end;

Regards
Roger Fedyk

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AswAuthor Commented:
Hi rfedyk,

Thanks for your comment, procedure SaveStringGrid works ok, but PopulateStringGrid has a problem.

 ListBox1.Items.Delete(r); //get rid of the strings that belong to the string grid is not deleting them, it is deleting every other on.

Thanks
Andy
AswAuthor Commented:
Hi rfedyk,

Should be  ListBox1.Items.Delete(c-1); //get rid of the strings that belong to the string grid.

Thanks
Asw
rfedykCommented:
Hi Andy

Sorry for the stuff up. I should not have used a for..loop. It only works properly when you use a repeat or while loop.

 r:=0;
 repeat
  ListBox1.Items.Delete(0);
  inc(r);
 until r=rl;


Thank you for the grade.

Regards
Roger
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.