Solved

Add data to a listview with a textfile

Posted on 2010-09-21
5
461 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 37

Accepted Solution

by:
Geert Gruwez earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now