Salut,
Tu peux écrire le nombre d'items de chaque ListBox dans le fichier ini.
Pour la lecture, la boucle mise en oeuvre utilisera ce compteur pour remplir chaque ListBox.
Pour être plus clair, voici un exemple qui reprend les éléments de ton code.
Écriture dans le fichier :
procedure TForm1.Button1Click(Sender: TObject);
var
F: TIniFile;
i: Integer;
begin
SaveDialog1.InitialDir := 'C:\MyProjects';
if SaveDialog1.Execute then
begin
F := TIniFile.Create(SaveDialog1.FileName + '.ini');
try
F.WriteInteger('Liste1', 'CountItems', ListBox1.Items.Count);
for i := 0 to ListBox1.Items.Count - 1 do
F.WriteString('Liste1', Format('ligne%d', [i]), ListBox1.Items[i]);
F.WriteInteger('Liste2', 'CountItems', ListBox2.Items.Count);
for i := 0 to ListBox2.Items.Count - 1 do
F.WriteString('Liste2', Format('ligne%d', [i]), ListBox2.Items[i]);
F.WriteInteger('Liste3', 'CountItems', ListBox3.Items.Count);
for i := 0 to ListBox3.Items.Count - 1 do
F.WriteString('Liste3', Format('ligne%d', [i]), ListBox3.Items[i]);
F.WriteInteger('Liste4', 'CountItems', ListBox4.Items.Count);
for i := 0 to ListBox4.Items.Count - 1 do
F.WriteString('Liste4', Format('ligne%d', [i]), ListBox4.Items[i]);
finally
F.Free;
end;
end;
end;
Lecture depuis le fichier :
procedure TForm1.Button2Click(Sender: TObject);
var
F: TIniFile;
i: Integer;
CountItems: Integer;
begin
OpenDialog1.InitialDir := 'C:\MyProjects';
if OpenDialog1.Execute then
begin
F := TIniFile.Create(OpenDialog1.FileName);
try
CountItems := F.ReadInteger('Liste1', 'CountItems', 0);
for i := 0 to CountItems - 1 do
ListBox1.Items.Add(F.ReadString('Liste1', Format('ligne%d', [i]), ''));
CountItems := F.ReadInteger('Liste2', 'CountItems', 0);
for i := 0 to CountItems - 1 do
ListBox2.Items.Add(F.ReadString('Liste2', Format('ligne%d', [i]), ''));
CountItems := F.ReadInteger('Liste3', 'CountItems', 0);
for i := 0 to CountItems - 1 do
ListBox3.Items.Add(F.ReadString('Liste3', Format('ligne%d', [i]), ''));
CountItems := F.ReadInteger('Liste4', 'CountItems', 0);
for i := 0 to CountItems - 1 do
ListBox4.Items.Add(F.ReadString('Liste4', Format('ligne%d', [i]), ''));
finally
F.Free;
end;
end;
end;
Et n'oublie pas de libérer le fichier ini !
Bon courage.
3 févr. 2019 à 09:52