• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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?
0
karen021897
Asked:
karen021897
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now