I need to load items from a text file into a Listview
using Opendialog
, how to prepend?
Is there any property LoadFromFile
?
I need to load items from a text file into a Listview
using Opendialog
, how to prepend?
Is there any property LoadFromFile
?
The LoadFromFile
method is not available in Listview
, what you can do is load the file information into StringList
and popular Listview
with TListItem
. Here's an example:
procedure AddItemsListview(Listview: TListView; Arquivo: string);
Var
Items: TStringList;
Item: TListItem;
I: Integer;
begin
Items := TStringList.Create;
try
Items.LoadFromFile(Arquivo);
for I := 0 to Items.Count -1 do begin
Item := Listview.Items.Add;
Item.Caption := Items[I]; // Coloca o valor na primeira coluna do Listview
end;
finally
Items.Free;
end;
end;
And to use the above method, assuming you've already added% with%, do the following:
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.InitialDir := ExtractFilePath(ParamStr(0)); // Abre o diálogo no diretório do programa
OpenDialog1.Filter := 'Arquivos de texto (.txt) | *.txt'; // Somente arquivos de texto
if OpenDialog1.Execute = false then exit;
AddItemsListview(ListView1, OpenDialog1.FileName);
end;