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?
 
Sinisa VukCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.