Solved

Drag/Drop to CheckListBox based on extension

Posted on 2002-07-21
7
265 Views
Last Modified: 2010-04-04
Hi -

I have an application where 3 CheckListBoxes, 1 DirectoryListBox and 1 FileListBox are on the form. The user should be able to drag and drop files on each CheckListBox based on file extensions only.

For Example:

CheckListBox1 - *.avi
CheckListBox2 - *.doc
CheckListBox3 - *.bmp/jpeg

Right now I have this:

procedure TMainForm.clBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  i: Integer;
  tmp: String;
begin
  if (Sender is TCheckListBox) and (Source is TDirectoryListBox) then
  begin
    with Sender as TCheckListBox do
    begin
      tmp := lbDir.GetItemPath(lbDir.ItemIndex);
      if Copy(tmp, Length(tmp), 1) = '\' then tmp := Copy(tmp, 1, Length(tmp)-1);
      Items.Add(tmp+'\');
      Checked[Items.Count-1] := True;
    end;
  end;
  if (Sender is TCheckListBox) and (Source is TFileListBox) then
  for i := 0 to (lbFiles.Items.Count - 1) do
    if lbFiles.Selected[i] then with Sender as TCheckListBox do
    begin
      tmp := lbDir.Directory;
      if Copy(tmp, Length(tmp), 1) = '\' then tmp := Copy(tmp, 1, Length(tmp)-1);
      Items.Add(tmp+'\'+lbfiles.Items[i]);
    end;

end;

procedure TMainForm.clBoxDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
 Accept := (Source is TDirectoryListBox) or (source is TFileListBox);
end;

This code is basically allowing any folder/file to be added to the list.

The reason for this is I'm building a CD-ROM burner APP where each file extension will go into its own folder, so when an main exe loads the files in each folder will be viewable by clicking on each tab.

TIA -

RB


0
Comment
Question by:routerboy309
[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
  • 4
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 100 total points
ID: 7168963
routerboy309,

Replace your clBoxDragDrop handler with this modified version to make each CheckListBox accept only certain file extensions:

procedure TForm1.clBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
var
 i: Integer;
 tmp: String;
 ext, OKext: String; // Added
begin
 if (Sender is TCheckListBox) and (Source is TDirectoryListBox) then
 begin
   with Sender as TCheckListBox do
   begin
     tmp := lbDir.GetItemPath(lbDir.ItemIndex);
     if Copy(tmp, Length(tmp), 1) = '\' then tmp := Copy(tmp, 1, Length(tmp)-1);
     Items.Add(tmp+'\');
     Checked[Items.Count-1] := True;
   end;
 end;
 if (Sender is TCheckListBox) and (Source is TFileListBox) then begin
    with (Sender as TCheckListBox) do begin
         // Define allowed extensions based on target name (no spaces allowed!):
         if Name='CheckListBox1' then OKext:='avi';
         if Name='CheckListBox2' then OKext:='doc';
         if Name='CheckListBox3' then OKext:='bmp;jpeg';
    end;
    for i := 0 to (lbFiles.Items.Count - 1) do
        if lbFiles.Selected[i] then with Sender as TCheckListBox do
        begin
          // File extension:
          ext:=LowerCase(ExtractFileExt(lbfiles.Items[i]));
          ext:=Copy(ext, 2, Length(ext));
          // Only add file with allowed extension for this CheckListBox:
          if (ext<>'') and (Pos(ext+';', OKext+';')>0) then begin
             tmp := lbDir.Directory;
             if Copy(tmp, Length(tmp), 1) = '\' then tmp := Copy(tmp, 1, Length(tmp)-1);
             Items.Add(tmp+'\'+lbfiles.Items[i]);
          end;
        end;
 end;
end;
0
 

Author Comment

by:routerboy309
ID: 7169162
That did it! Thanks. You'd be surprise the mess I had trying to do what you did in very little code.  :)

RB

0
 
LVL 7

Expert Comment

by:Cynna
ID: 7169172
:)
You are welcome.
0
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!

 

Author Comment

by:routerboy309
ID: 7182967
Cynna

Is there a simple solution to adding the ability to permit drag and drop from Windows Explorer and allow only certain extensions to be dragged to certain listboxes like the application offers?

RB
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7183302
I'm arfraid not. You should try using some
of existing free components, and implement the
logic I used to filer file types.

For example:
Drag & Drop Component Suite : http://www.users.on.net/johnson/delphi/

Lot's of free drag'n drop components: http://www.torry.net/draganddrop.htm

0
 

Author Comment

by:routerboy309
ID: 7183311
Ok - Thanks for responding (esp. since this thread is a closed one).

RB
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7183371
No problem.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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