Solved

Drag/Drop to CheckListBox based on extension

Posted on 2002-07-21
7
251 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
  • 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now