Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to associate icons for different files in a listview

Posted on 2005-03-26
10
Medium Priority
?
460 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

579 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