?
Solved

How to associate icons for different files in a listview

Posted on 2005-03-26
10
Medium Priority
?
452 Views
Last Modified: 2010-04-05
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 !
0
Comment
Question by:kyrlean
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13637666
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


0
 
LVL 3

Author Comment

by:kyrlean
ID: 13637694
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 !
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13637720
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;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:kyrlean
ID: 13637762
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 ???
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 1000 total points
ID: 13637937
The code you have seems to decode a string that has tags in it to distiguish data.

It looks like the buffer is in the format of someting like:
<LISTOFFILES>12|File1|File2|..etc..|File12|
So he has made up this string before it gets sent over to the socket.
What you want to do is build up this string and send it through the buffer

First you need to get the files from the directory on the remote pc, build up the string, then send it through the buffer for the OnRead event to decode.

procedure TForm1.Button1Click(Sender: TObject);
var
  iLoop: Integer;
  lsti: TListItem;
  sBuffer: String;
begin
  try
    // create stringlist
    MyFiles := TStringList.Create;
    // get your list of files from a specified directry
    MyFiles.Text := GetDirFiles('c:\','*.*');
    // start to build up the string you are going to send
    sBuffer := <LISTOFFILES>
    // add on the file count plus his marker
    sBuffer := sBuffer + IntToStr(MyFiles.Count) + '|';
    // loop through all the file names
    For iLoop := 0 to MyFiles.Count -1 do
    begin
      // add on a file and a break
      sBuffer := sBuffer + MyFiles[iLoop] + '|'
    end;
    // finally send the buffer to the other pc
    ClientSocket.Socket.SendText(sBuffer)  // think it is something like this off the top of my head
  finally
    // free the stringlist
    FreeAndNil(MyFiles)
  end;
end;

You might have to adjust the OnRed procedure you showed me. Im sorta guessing at his decoding so you might have to step through it yourself and watch
Ive changed a bit of the code below, hopefully you will be able to figure out the rest

if pos('<LISTOFFILES>',buf)=1 then
  begin
  // strip off the tag <listoffiles>
  delete(buf,1,13);
  // strip out the count of files
  DirCount:=StrToInt(copy(buf,1,pos('|',buf)-1));
  // get rid of first break
  delete(buf,1,pos('|',buf));
  // clear listviews
   FrmFileManager.ListView1.Clear;
   FrmFileManager.ListView2.Clear;
  // loop through the count
  for I:=1 to DirCount do begin
  // grab a file name
  DirName:=Copy(buf,1,pos('|',buf)-1);
  // strip off to the next break
  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;
0
 
LVL 3

Author Comment

by:kyrlean
ID: 13637978
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).
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13637998
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
0
 
LVL 3

Author Comment

by:kyrlean
ID: 13638006
I got it buddy, thank you very much ! Get he points, you deserve it ! !
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13638007
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
0
 
LVL 3

Author Comment

by:kyrlean
ID: 13638014
that's it ! it's all done, i just would like to change its' visual aspect. Thanks again !
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question