kyrlean
asked on
How to associate icons for different files in a listview
Hi all; i'm building a file transfer using the classic Client/Server conception. The ideia is ...i have the application running on 2 different machines; the local and remote one,both will act as clients and servers as well,and both sides will be able to browse each other.My question is... what should i do to display (in a listview) different icons for different types of files ??? Currently i have only one icon (image) for all the files in it. Thanks in advance !
ASKER
Hi mike, thanks a lot, let me try it, i have to figure now, how to insert it into the existing codes.
I'll get back to you !
I'll get back to you !
Here is some start code that might help you
I am using a global stringlist called MyFiles
// This first procedure will return the file names from a specific directory (without path)
function TForm1.GetDirFiles(Path, FileSpec: String): String;
var
szFileSpec: String;
srFind: TSearchRec;
dwFind: Integer;
begin
// Set default result
result:='';
// Build full file spec
szFileSpec:=Format('%s\%s' , [ExcludeTrailingBackslash( Path), FileSpec]);
// Start the find
dwFind:=FindFirst(szFileSp ec, faAnyFile, srFind);
while (dwFind = 0) do
begin
// Check for files only (no sub dirs)
if ((srFind.Attr and faDirectory) = 0) then
begin
// Add to list
MyFiles.Add(srFind.Name)
end;
// Find next
dwFind:=FindNext(srFind);
end;
// Close the find
FindClose(srFind);
// return the list
Result := MyFiles.Text;
end;
// I just used a button click to go get the files from the root of C: and add them to a listview
procedure TForm1.Button1Click(Sender : TObject);
var
iLoop: Integer;
lsti: TListItem;
begin
try
// create stringlist
MyFiles := TStringList.Create;
// get your list of files from a specified directry
MyFiles.Text := GetDirFiles('c:\','*.*');
// loop through all the file names
For iLoop := 0 to MyFiles.Count -1 do
begin
// add a new listview item
lsti := ListView1.Items.Add;
// now do your file name checking
if Pos('.doc', MyFiles[iLoop]) > then
// change image
lsti.ImageIndex := 4 // or whatever you need
else
if Pos('.bmp', MyFiles[iLoop]) > then
// change image
lsti.ImageIndex := 5 // or whatever you need
// etc etc
end;
finally
// free the stringlist
FreeAndNil(MyFiles)
end;
end;
I am using a global stringlist called MyFiles
// This first procedure will return the file names from a specific directory (without path)
function TForm1.GetDirFiles(Path, FileSpec: String): String;
var
szFileSpec: String;
srFind: TSearchRec;
dwFind: Integer;
begin
// Set default result
result:='';
// Build full file spec
szFileSpec:=Format('%s\%s'
// Start the find
dwFind:=FindFirst(szFileSp
while (dwFind = 0) do
begin
// Check for files only (no sub dirs)
if ((srFind.Attr and faDirectory) = 0) then
begin
// Add to list
MyFiles.Add(srFind.Name)
end;
// Find next
dwFind:=FindNext(srFind);
end;
// Close the find
FindClose(srFind);
// return the list
Result := MyFiles.Text;
end;
// I just used a button click to go get the files from the root of C: and add them to a listview
procedure TForm1.Button1Click(Sender
var
iLoop: Integer;
lsti: TListItem;
begin
try
// create stringlist
MyFiles := TStringList.Create;
// get your list of files from a specified directry
MyFiles.Text := GetDirFiles('c:\','*.*');
// loop through all the file names
For iLoop := 0 to MyFiles.Count -1 do
begin
// add a new listview item
lsti := ListView1.Items.Add;
// now do your file name checking
if Pos('.doc', MyFiles[iLoop]) > then
// change image
lsti.ImageIndex := 4 // or whatever you need
else
if Pos('.bmp', MyFiles[iLoop]) > then
// change image
lsti.ImageIndex := 5 // or whatever you need
// etc etc
end;
finally
// free the stringlist
FreeAndNil(MyFiles)
end;
end;
ASKER
Cool ! i took a quick look and i'll study it carefully to fully understand. just let me show you something and make things easyer....
on my socket OnRead event i have the following piece of code which will bring to me the remote files and folders...I'm a real beginner and this was provided by a friend so i understand the codes but i have no ability to manipulate it the way i want. let's see.....
if pos('<LISTOFFILES>',buf)=1 then
begin
delete(buf,1,25);
DirCount:=StrToInt(copy(bu f,1,pos('| ',buf)-1)) ;
delete(buf,1,pos('|',buf)) ;
FrmFileManager.ListView1.C lear;
FrmFileManager.ListView2.C lear;
for I:=1 to DirCount do begin
DirName:=Copy(buf,1,pos('| ',buf)-1);
delete(buf,1,pos('|',buf)) ;
if (DirName <> '.') and (DirName <> '..') then
FrmFileManager.ListView1.A ddItem(Dir Name,point er(self));
FrmFileManager.ListView1.U pdate;
end;
delete(buf,1,pos(':',buf)) ;
FileCount:=strtoint(copy(b uf,1,pos(' |',buf)-1) );
delete(buf,1,pos('|',buf)) ;
for i :=1 to FileCount do begin
FrmFileManager.ListView2.U pdate;
FileName:=copy(buf,1,pos(' |',buf)-1) ;
delete(buf,1,pos('|',buf)) ;
FrmFileManager.ListView2.A ddItem(Fil eName,poin ter(self)) ;
end;
Could we drop your codes here ???
on my socket OnRead event i have the following piece of code which will bring to me the remote files and folders...I'm a real beginner and this was provided by a friend so i understand the codes but i have no ability to manipulate it the way i want. let's see.....
if pos('<LISTOFFILES>',buf)=1
begin
delete(buf,1,25);
DirCount:=StrToInt(copy(bu
delete(buf,1,pos('|',buf))
FrmFileManager.ListView1.C
FrmFileManager.ListView2.C
for I:=1 to DirCount do begin
DirName:=Copy(buf,1,pos('|
delete(buf,1,pos('|',buf))
if (DirName <> '.') and (DirName <> '..') then
FrmFileManager.ListView1.A
FrmFileManager.ListView1.U
end;
delete(buf,1,pos(':',buf))
FileCount:=strtoint(copy(b
delete(buf,1,pos('|',buf))
for i :=1 to FileCount do begin
FrmFileManager.ListView2.U
FileName:=copy(buf,1,pos('
delete(buf,1,pos('|',buf))
FrmFileManager.ListView2.A
end;
Could we drop your codes here ???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok i think it's clear now , just making sure ! i may have to change the onread but in fact what will make the listview2 displays different images depends on the information the server is sending me.
2 - so.. i have to make changes there right? just a little more of codes to get a little more of information. Is that it ? if so, i think i can go on by myself ! Thanks Mike (just waiting your last reply since i can't really try it right now).
2 - so.. i have to make changes there right? just a little more of codes to get a little more of information. Is that it ? if so, i think i can go on by myself ! Thanks Mike (just waiting your last reply since i can't really try it right now).
You will have to write your own decode function to add the correct image to each of the items.
You will need to grab the file extension from the data that is passed to you
He uses the line
FrmFileManager.ListView2.A ddItem(Dir Name,point er(self)); // not sure what listview is doing what
but if you want a bit more control, replace it with the code below and declare lsti: TListItem
lsti := FrmFileManager.ListView2.I tems.Add
lsti.Caption := DirName;
// now do your file extension check
if Pos('.doc', lsti.Caption) > 0 then
// set image
lsti.ImageIndex := x
etc etc
You will need to grab the file extension from the data that is passed to you
He uses the line
FrmFileManager.ListView2.A
but if you want a bit more control, replace it with the code below and declare lsti: TListItem
lsti := FrmFileManager.ListView2.I
lsti.Caption := DirName;
// now do your file extension check
if Pos('.doc', lsti.Caption) > 0 then
// set image
lsti.ImageIndex := x
etc etc
ASKER
I got it buddy, thank you very much ! Get he points, you deserve it ! !
I suppose the general idea here is ...
1) Get a list of file names from a directory
2) Build up a text string with specific information i.e. "<LISTOFFILES>3|File1|File 2|File2"
3) Send text over the socket
4) Read at other socket
5) Strip off text identity tag
6) Get file count and strip off text once got
7) Loop through rest of string getting all file names (you know how many times to loop as you know the file count)
8) Add file name to listview
9) Check file extension as you add to listview and assign imageindex from a TImageList
Hope this all helps
1) Get a list of file names from a directory
2) Build up a text string with specific information i.e. "<LISTOFFILES>3|File1|File
3) Send text over the socket
4) Read at other socket
5) Strip off text identity tag
6) Get file count and strip off text once got
7) Loop through rest of string getting all file names (you know how many times to loop as you know the file count)
8) Add file name to listview
9) Check file extension as you add to listview and assign imageindex from a TImageList
Hope this all helps
ASKER
that's it ! it's all done, i just would like to change its' visual aspect. Thanks again !
Then as you add an item to the listview, just check the file name for the specific extension.
ExtractFileExt function will return the extension of a file you specify as a string.
Then use look for the extension and choose the image.
var
sExt: String;
begin
sExt := ExtractFileExt('c:\Doc.doc
// returns .doc
if sExt = '.doc' then
// assign image
ListView.Item[x].ItemIndex