How to populate ListView control?

Hi,


I'm using D7 Ent. on a WinXP Pro SP3 box.

I've place a ListView1 control on a panel.  I am able to add headings and set column width, but do not understand how to add "data" under the headings.

My ListView1 has 3 columns.
1.   File Name, Original Size, Reduced Size.
2.   I want to populate the first two columns when I click Button1 (file name & file size).  
3.  Next, click Button2
     a. Do some work to do some work (procedure call which returns data for col 3. (file size after compression))
     b. Populate the 3rd column with the results of step 3.a..

Can I do this in two steps? How do I do it?

My code, so far, is below.

Thanks much!
.... 
procedure ListFiles(const AFolder, AFilter: string; AList: TStrings);

var
  NewColumn: TListColumn;
  ListItem: TListItem;

implementation
...
...
...
procedure TfResizeFiles.btnGetSetClick(Sender: TObject);
var
  sl : TStringList;
  strPath, strFilter : string;
  i  : integer;
  NewColumn: TListColumn;
  ListItem: TListItem;
begin
  strPath := Trim(edDrivePath.Text);  /have deleted code to verify directory exists, etc.
try  
  with ListView1 do
  begin
    ViewStyle := vsReport;

    NewColumn := Columns.Add;
    NewColumn.Width := 250;
    NewColumn.Caption := 'Image Name';
    NewColumn := Columns.Add;
    NewColumn.Width := 100;
    NewColumn.Caption := 'Original Size';
    NewColumn := Columns.Add;
    NewColumn.Width := 100;
    NewColumn.Caption := 'Reduced Size';
  end;
  sl := TStringList.Create;
   ListFiles(strPath, strFilter, sl.add.item)  // procedure is below.
 finaly
  sl.Free;
 end;
end;
===============================
procedure ListFiles(const AFolder, AFilter: string; AList: TStrings);
var
  vFindHandle: THandle;
  vFilter    : String;
  vFindData  : WIN32_FIND_DATA;
begin
  AList.BeginUpdate;
  try
    AList.Clear;
    vFilter := AFolder + '\' + AFilter;
    vFindHandle := FindFirstFile(PChar(vFilter), vFindData);
    if vFindHandle = INVALID_HANDLE_VALUE then
      Exit;

    repeat
      AList.Add(ChangeFileExt(vFindData.cFileName, ''));
    until not FindNextFile(vFindHandle, vFindData);

    Windows.FindClose(vFindHandle);
  finally
    AList.EndUpdate;
  end;
end;

Open in new window

LVL 1
IT79637Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sinisa VukSoftware architectCommented:

First, set ViewStyle proprety to vsReport and columns in design mode,
second, do not use global variables:

  NewColumn: TListColumn;
  ListItem: TListItem;

Open in new window


(defined before implementation)

third, your code looks like now:

procedure TfResizeFiles.btnGetSetClick(Sender: TObject);
var
  sl : TStringList;
  strPath, strFilter : string;
  i  : integer;
  ListItem: TListItem;
begin
  strPath := Trim(edDrivePath.Text);
  strFilter := '*.txt';
try
   sl := TStringList.Create;
   ListFiles(strPath, strFilter, sl);

   ListView1.Clear;
   for i := 1 to sl.Count do
   begin
     ListItem := ListView1.Items.Add;
     ListItem.Caption := sl.Strings[i-1]; //always first column
     ListItem.SubItems.Add('123');        //add second column (get file size)
     ListItem.SubItems.Add('');           //add third column - empty to easy replace after
   end;
 finally
   sl.Free;
 end;
end;

procedure TfResizeFiles.Button2Click(Sender: TObject);
var
  ListItem: TListItem;
begin
  if ListView1.ItemIndex>=0 then //selected
  begin
    ListItem := ListView1.Items.Item[ListView1.ItemIndex];
    ListItem.SubItems.Strings[1] := '456'; //change third column; 0=second column
  end;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.