Solved

Add data to a listview with a textfile

Posted on 2010-09-21
5
460 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

10 Experts available now in Live!

Get 1:1 Help Now