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
Solved

Drag/Drop to CheckListBox based on extension

Posted on 2002-07-21
7
260 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
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.

 

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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