save tlisbox content to inifile

Tags:    delphi github

hello

I have a problem

I need to save all the contents of a listbox to an inifile...

Please a code snippet ...

best regards

hjalmar




1 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
A solution:

<pre>
Uses
Inifiles;

procedure ListBoxTilIni(AListbox: TListBox);
var
Ini: TIniFile;
i: byte;
ItmCnt: Integer;
begin
ItmCnt := AListBox.Items.Count;
Ini := TIniFile.Create('c:\\temp.ini');
try
Ini.WriteInteger('Main', 'cnt', ItmCnt);
for i := 0 to pred(ItmCnt) do
begin
Ini.WriteString('Main', 'i' +Inttostr(i), AlistBox.Items.Strings);
end;
finally
Ini.Free;
end;
end;

</pre>

Call with this: (A button and a Listbox is placed on the form and an inifile called temp.ini placed on c:\\)

<pre>

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBoxTilIni(ListBox1);
end;

</pre>

To read the items from the same inifile to the same listbox or another one, use this:

<pre>
procedure IniTilListBox(AListBox: TListBox);
var
Ini: TIniFile;
i: byte;
ItmCnt: Integer;
begin
AListBox.Clear;
Ini := TIniFile.Create('c:\\temp.ini');
try
ItmCnt := Ini.ReadInteger('Main', 'cnt', 0);
for i := 0 to pred(ItmCnt) do
begin
AListBox.Items.Add(Ini.ReadString('Main', 'i' +Inttostr(i), ''))
end;
finally
Ini.Free;
end;
end;
</pre>

and to call it:

<pre>

procedure TForm1.Button1Click(Sender: TObject);
begin
IniTilListbox(ListBox1);
end;

</pre>[Redigeret d. 02/04-03 16:44:43 af Casper Steinmann]




t