Solved

Add data to a listview with a textfile

Posted on 2010-09-21
5
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 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