Load listview lines from an Opendialog

Asked

Viewed 863 times

1

I need to load items from a text file into a Listview using Opendialog, how to precede?

Is there any property LoadFromFile?

1 answer

1


The method LoadFromFile is not available on Listview, what you can do is upload the file information to a StringList and popular the Listview with TListItem. Take 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 have already added OpenDialog, 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;
  • It worked perfectly, but could I limit the amount of items in this listview? for example, 600 items it only load 200?

  • @Fake No loop for which is done in the method AddItemsListview, you can do something like this: if I = 200 then Break;. This will add only the 200 first items to Listview.

  • 1

    Perfect, thank you very much.

  • It would have to limit the items of Tstringlist?

  • @Fake I believe it is not possible, the documentation at least it doesn’t say anything about it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.