write all in listbox into "ini" file and vice versa

when i close the app i will be able to write all text that are in listbox into "ini" file, and when i open the app i will read from "ini" file into listbox. Is this possible in Delphi?
karen021897Asked:
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.

intheCommented:
hello again
will this be ok:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,inifiles;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Inifile : tinifile;
  ii : integer;
  implementation

uses Unit2;

{$R *.DFM}


procedure TForm1.FormCreate(Sender: TObject);
var
i : integer;
s : string;  
sl : tstringlist;
begin
if fileexists('c:\mylist.ini') then begin
sl := tstringlist.create;
sl.loadfromfile('c:\mylist.ini');
for i := 1 to sl.count -1  do begin
S:= sl.Strings[i];
Listbox1.Items.add(Copy(S, Pos('=',S)+1, maxint));
end; //for
sl.free;
end;//fileexist

end;

procedure TForm1.FormDestroy(Sender: TObject);
var
 i : integer;
begin
IniFile:=Tinifile.Create('c:\mylist.ini');
for i := 0 to listbox1.Items.Count -1
do begin
IniFile.WriteString('listitems','item'+inttostr(i),listbox1.Items[i]);
end;
InIfile.Free;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
//for testing
listbox1.Items.Loadfromfile('c:\autoexec.bat');
end;

end.


Regards Barry

0

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
intheCommented:
oops ,dont need
ii : integer; line it just was me testing stuff
0
gwillsonCommented:
You don't have to use an inifile for this, but you sure can if you want to.
Perhaps a simpler way of doing this is to use the SaveToFile and LoadFromFile functions.  Here is a little example that you can try on a form.  Just drop a list box on the form and copy the following code.

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ListBox1.Items.SaveToFile('C:\test.txt');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  if FileExists('C:\test.txt') then  //since the file doesn't exist the first time you run it
    ListBox1.Items.LoadFromFile('C:\test.txt');
end;
0
karen021897Author Commented:
Thanks Again!!!
0
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.