Link to home
Start Free TrialLog in
Avatar of kyrlean
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 !
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

You could use an image list to hold all the different types of images you want to associate.
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 := 4  // or whatever you need it to be


Avatar of kyrlean
kyrlean

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 !
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(szFileSpec, 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;
Avatar of kyrlean

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(buf,1,pos('|',buf)-1));
  delete(buf,1,pos('|',buf));
   FrmFileManager.ListView1.Clear;
   FrmFileManager.ListView2.Clear;
  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.AddItem(DirName,pointer(self));
  FrmFileManager.ListView1.Update;
  end;
   delete(buf,1,pos(':',buf));
   FileCount:=strtoint(copy(buf,1,pos('|',buf)-1));
    delete(buf,1,pos('|',buf));

    for i :=1 to FileCount do begin
    FrmFileManager.ListView2.Update;
    FileName:=copy(buf,1,pos('|',buf)-1);
    delete(buf,1,pos('|',buf));
    FrmFileManager.ListView2.AddItem(FileName,pointer(self));
   
    end;

Could we drop your codes here ???
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kyrlean

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).
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.AddItem(DirName,pointer(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.Items.Add
lsti.Caption := DirName;
// now do your file extension check
if Pos('.doc', lsti.Caption) > 0 then
  // set image
  lsti.ImageIndex := x

etc etc
Avatar of kyrlean

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|File2|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
Avatar of kyrlean

ASKER

that's it ! it's all done, i just would like to change its' visual aspect. Thanks again !