Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Add data to a listview with a textfile

Posted on 2010-09-21
5
Medium Priority
?
468 Views
Last Modified: 2012-05-10
Hi,

Can someone tell me how to add data to a listview
for example with a textfile. Because I need to load
a lot of data and I don't want to use the property Items
all the time.

For the record, I am using a simple listview, I am not
using Groups anymore like I did in my previous question.

Peter
0
Comment
Question by:peterkiers
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 33723824
Hi,

Do you mean something like this:

procedure AddListViewItemsFromFile(pFileName: String);
var
  tstrTmp: TStringList;
  iX: Integer;
  liTmp: TListItem;
begin
  tstrTmp := TStringList.Create;
  tstrTmp.LoadFromFile( pFileName );
  for iX:=0 to tstrTmp.Count-1 do
  begin
    liTmp := ListView1.Items.Add;
    liTmp.Caption := tstrTmp[iX];
  end;
  FreeandNil( tstrTmp );
end;



Example of usage:
AddListViewItemsFromFile( 'C:\Test.txt' );


Best regards,

Wim.
0
 
LVL 6

Expert Comment

by:wimmeyvaert
ID: 33723844
Or, a more generic version of the procedure:

procedure AddListViewItemsFromFile( pFileName: String  ; pListView: TListView );
var
    tstrTmp: TStringList;
    iX: Integer;
    liTmp: TListItem;
begin
    tstrTmp := TStringList.Create;
    tstrTmp.LoadFromFile( pFileName );
    for iX:=0 to tstrTmp.Count-1 do
    begin
        liTmp := pListView.Items.Add;
        liTmp.Caption := tstrTmp[iX];
    end;
    FreeandNil( tstrTmp );
end;



And use it like this:
AddListViewItemsFromFile( 'C:\Test.txt', lvYourListView );
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33724296
How should the code be when the programm has to look in its own directory for the test.txt

AddListViewItemsFromFile( 'C:\Test.txt', lvYourListView );
0
 
LVL 1

Author Comment

by:peterkiers
ID: 33724338
Ignore the above question

But I want to put data in the listview with a text-file
and use a delimiter for put the data in the columns.
How can I do that?
0
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 2000 total points
ID: 33724742

procedure AddListViewItemsFromFile( pFileName: String  ; pListView: TListView );
var
  tstrTmp, sublist: TStringList;
  iX: Integer;
  liTmp: TListItem;
begin
  tstrTmp := TStringList.Create;
  try
    tstrTmp.LoadFromFile( pFileName );
    sublist := TStringList.Create;
    try
      sublist.Delimiter := '|'; // pipe symbol as delimiter
      for iX:=0 to tstrTmp.Count-1 do
      begin
        sublist.DelimitedText := tstrTmp[iX];
        liTmp := pListView.Items.Add;
        liTmp.Caption := sublist[0];
        for iY := 1 to sublist.Count-1 do
          liTmp.SubItems.Add(subList[iY]);
      end;
    finally
      sublist.Free;
    end;
  finally
    FreeandNil( tstrTmp );
  end;
end;
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question