Solved

Drag/Drop to CheckListBox based on extension

Posted on 2002-07-21
7
255 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi with SQL Natvie Client 15 86
Convert Jpg /PNG To GIF 5 120
indy Tmemory stream non latin charcter issue 2 98
Delphi Mdi application Child forms get behind control 7 154
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…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

23 Experts available now in Live!

Get 1:1 Help Now