Solved

Add data to a listview with a textfile

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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