Solved

Using MultiSelect in ListBox

Posted on 1997-11-26
6
1,445 Views
Last Modified: 2010-04-04
I've got a FileListBox and a regular ListBox. I've got MultiSelect set to true in the FileListBox. I simply want to be able to select multiple files from the FileListBox and when I click the RIGHT mouse button, have those files go into my ListBox. I've got the right mouse button click event thing figured out, but when I select multiple files from my FileListBox and perform the action, all I get in my ListBox is the last file selected, not all of them like I want. Here's my code so far:

procedure TForm1.FileListBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbRight then
  begin
    ListBox1.Items.Add(FileListBox1.FileName);
  end;
end;

.. So how can I add ALL the filenames I select to my ListBox, not just the last one of the bunch selected? What am I missing here?

Thanks,
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
  • 4
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
anilms earned 50 total points
ID: 1352024
Well, you missed out two things - you should check for the SelCount and then you must iterate through the Source list, check whether it is selected and if it has been selected, add it to the Destination list.

I give an example below where a multiselect from a ListBox to a Memo is made possible through press of a button. Have the joy of making it work with a file list :

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
     {Checking whether a multiselect has been performed}
     if ListBox1.SelCount>1 then
     for i:=0 to ListBox1.Items.Count-1 do
         if ListBox1.Selected[i]
             then Memo1.Lines.Add(ListBox1.Items[i]);

end;

0
 

Author Comment

by:aztec
ID: 1352025
Hello Anilms...
  Thanks for leaving me the joy of getting it to work with a file list... I couldn't. The only problem is I'm having trouble using the 'FileName' property to add the fully qualified filename to my listbox. Here's what I've got:

if Button = mbRight then
  begin
    if FileListBox1.SelCount > 1 then
       for i:=0 to FileListBox1.Items.Count-1 do
           if FileListBox1.Selected[i] then
   ListBox1.Items.Add(FileListBox1.items[i].FileName);
  end;

..This doesn't work. It doesn't like the 'FileName' after 'items[i]'. I NEED to have the fully qualified filename and pathname in the ListBox, so it would appear I need to use the 'FileName' property in some way....but how??

Thanks,
   Shawn Halfpenny
0
 
LVL 1

Expert Comment

by:anilms
ID: 1352026
Quite simple if you want the full path - this is what you do :

ListBox1.Items.Add(FileListBox1.Directory+FileListBox1.Items[i])

If you add the directory property, you get the full path. If you have still further doubts, email me at anilms@justsystem.co.jp
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:anilms
ID: 1352027
Ps : I missed out a '\'.

ListBox1.Items.Add(FileListBox1.Directory+'\'+FileListBox1.Items[i])
0
 

Author Comment

by:aztec
ID: 1352028
anilms...
   Did you get my e-mail from earlier today? Your suggestion is not quite right. It puts two '\' wheever you select files from a root directory like C:\. How to solve this?

Regards,
  Shawn Halfpenny
  drumme59@sprint.ca
0
 
LVL 1

Expert Comment

by:anilms
ID: 1352029
There is a way out. Just check what is the last character in FileListBox.Directory. If it is not a '\', then add it.

if FileListBox1.Directory[length(FileListBox1.Directory)]<>'\' then
 FileListBox1.Directory := FileListBox1.Directory+'\';

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

791 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