Hola.
Un modo de lograr lo que buscas de manera bastante sencilla es usando un array dinámico:
Código Delphi
[-]
...
implementation
const
CFILENAME = 'CODPOST.DAT';
type
TCodPost = record
clave : Integer;
ciudad : string[50];
end;
TFileCodPost = file of TCodPost;
TVecCodPost = array of TCodPost;
var
FCodPost : TFileCodPost;
VCodPost : TVecCodPost;
procedure GetCodPost(var F: TFileCodPost; var V: TVecCodPost; TS: TStrings);
var
CP : TCodPost;
begin
Reset(F);
while not eof(FCodPost) do
begin
Read(F, CP);
SetLength(V, Length(V)+1);
VCodPost[Length(V)-1].clave := CP.clave;
VCodPost[Length(V)-1].ciudad := CP.ciudad;
TS.Add('CODIGO POSTAL: ' + FormatFloat('0000', CP.clave) +
' - CIUDAD: ' + CP.ciudad);
end;
CloseFile(F);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AssignFile(FCodPost, CFILENAME);
GetCodPost(FCodPost, VCodPost, ListBox1.Items);
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
with ListBox1 do
if ItemIndex <> 0 then
begin
Edit1.Text := FormatFloat('0000', VCodPost[ItemIndex].clave);
Edit2.Text := VCodPost[ItemIndex].ciudad;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SetLength(VCodPost, 0);
VCodPost := nil;
end;
También podes usar
TStrings o manipular la cadena de caracteres del ListBox, ...
Lamentablemente al usar un record, no se puede aprovechar el método
Items.AddObject (TStrings) del ListBox.
Saludos.