I'm building a program that needs to on Form_Create
, populate a TListView
called FileList
, the directory that I want to populate is where the compiled program is + \Files
, as I never used a TListView
I want to know how to do this?
views:
464answers:
3You'll need to show the images when drawing the rows.
This should give you an idea: http://www.delphidabbler.com/articles?article=16 http://delphi.about.com/od/delphitips2008/qt/lv_checkbox_bmp.htm
The only difference is that you'll draw an icon/image. Here you learn how to do it in a grid: http://delphi.about.com/library/weekly/aa032205a.htm So from both you can get the idea.
If you drop a TImagelist on the form with two images in (one fore files and on for directories), then assign the TImagelist to the TListviews LargeImages property, you can use the below code.
procedure TForm2.Button1Click(Sender: TObject);
var li:TListItem;
SR: TSearchRec;
begin
FileList.Items.BeginUpdate;
try
FileList.Items.Clear;
FindFirst(ExtractFilePath(Application.ExeName) +'*.*', faAnyFile, SR);
try
repeat
li := FileList.Items.Add;
li.Caption := SR.Name;
if ((SR.Attr and faDirectory) <> 0) then li.ImageIndex := 1
else li.ImageIndex := 0;
until (FindNext(SR) <> 0);
finally
FindClose(SR);
end;
finally
FileList.Items.EndUpdate;
end;
end;
You can then build on this by adding different images to the TImageList for different file types, and using ExtractFileExt(SR.Name) to get the files extension
if ((SR.Attr and faDirectory) <> 0) then li.ImageIndex := 1
else if lowercase(ExtractFileExt(SR.Name)) = '.png' then li.ImageIndex := 2
else if lowercase(ExtractFileExt(SR.Name)) = '.pdf' then li.ImageIndex := 3
else li.ImageIndex := 0;
There are multiple parts to your question. I'll provide an overview here. If you need help on any particular step, please post a more specific follow-up question.
Determine what "where the compiled program is" refers to.
To get the full path of the EXE file, call
ParamStr(0)
. To remove the EXE file name from that string, so you have just the directory portion, callExtractFilePath
. Make sure it ends with a backslash (IncludeTrailingPathDelimiter
) and then append your "Files" directory.Get a list of files.
Use
FindFirst
andFindNext
to make a loop that looks at all the files. The names will include the "." and ".." relative directory names, so you may wish to exclude them. Beware that the files are not enumerated in any particular order. If you need them sorted alphabetically, for instance, you'll need to do that yourself. (They may appear to be in alphabetical order in your tests, but that won't always be true.)var Rec: TSearchRec; begin if FindFirst(path + '\*', faAnyFile, Rec) = 0 then try repeat if (Rec.Name = '.') or (Rec.Name = '..') then continue; if (Rec.Attr and faVolumeID) = faVolumeID then continue; // nothing useful to do with volume IDs if (Rec.Attr and faHidden) = faHidden then continue; // honor the OS "hidden" setting if (Rec.Attr and faDirectory) = faDirectory then ; // This is a directory. Might want to do something special. DoSomethingWithFile(Rec.Name); until FindNext(Rec) <> 0; finally SysUtils.FindClose(Rec); end; end;
Add nodes to the control to represent the files.
You might wish to do this in the hypothetical
DoSomethingWithFile
function I mentioned above. Use the list view'sItems
property to do things with the items, such as add new ones.var Item: TListItem; begin Item := ListView.Items.Add; Item.Caption := FileName; end;
TListItem
has additional properties; check the documentation for details. TheSubItems
property is useful if you're showing the list view in "report" mode, where there can be multiple columns for a single node.Associate images with the items.
Nodes' images in a list view come from the associated image lists,
LargeImages
andSmallImages
. They refer to one or moreTImageList
components on your form. Put your icon images there, and then assign the items'ImageIndex
properties to the corresponding numbers.
Depending on how elaborate you want your program to be, you may wish to use Delphi's TShellListView
control instead of doing all the above work yourself.