Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drag/Drop to CheckListBox based on extension

Posted on 2002-07-21
7
Medium Priority
?
293 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 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
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, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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