Drag & Drop using TFileListBox and TDirectoryListBox

I want to "drag & drop" files from a TFileListBox into different folders in my TDirectoryListBox.

I have seen examples of programs and have asked this question before but no one has answered with a concise answer. I need an example that shows me how to select the file and pull it into the other control and then have the file actually be 'MOVED' to the new location.

Every example I have seen will show how to implement the DragDrop procedure but there is never any "good" code behind it to actually perform the action.

I am using Delphi 3.0 Professional.

Thanks In Advance,

mjbcomp
mjbcompAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mjbcompAuthor Commented:
Edited text of question.
0
kretzschmarCommented:
hi mjbcomp,

a sample

unit drag_files_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  FileCtrl, StdCtrls;

type
  TForm1 = class(TForm)
    FileListBox1: TFileListBox;
    DirectoryListBox1: TDirectoryListBox;
    procedure FileListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FileListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure DirectoryListBox1DragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
    procedure DirectoryListBox1DragDrop(Sender, Source: TObject; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var Drgx,DrgY : Integer;

//initiate drag if mouse moved by 10 pixels and button was pressed
procedure TForm1.FileListBox1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  if ssLeft in Shift then
    If (abs(x-drgx) > 10) or
       (abs(y-drgy) > 10) then
      FileListBox1.BeginDrag(True);
end;

procedure TForm1.FileListBox1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ssleft in Shift then
  begin
    DrgX := X;
    DrgY := Y;
  end;
end;

procedure TForm1.DirectoryListBox1DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
  If source = FileListBox1 then
    Accept := True;
end;

procedure TForm1.DirectoryListBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  I,J : Integer;
  S,D : String;
begin
  I := DirectoryListBox1.ItemAtPos(Point(x,y),True);
  If I > -1 then
  Begin
    if I <> DirectoryListBox1.ItemIndex then
    Begin
      S := DirectoryListBox1.GetItemPath(I)+'\';
      D := DirectoryListBox1.GetItemPath(DirectoryListBox1.ItemIndex)+'\';
      For J := 0 to FileListBox1.Items.Count-1 do
       if FileListBox1.Selected[J] then
         if MoveFileEx(PChar(S+FileListBox1.Items[J]),PChar(D+FileListBox1.Items[J]),
                       MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING or MOVEFILE_WRITE_THROUGH) then
           ShowMessage(IntToStr(GetLastError));
      FileListBox1.Update;
    end;
  end;
end;

end.

meikl
0
mjbcompAuthor Commented:
hello kretzschmar,

This code is excellent.
Could you provide me with the additional code that will actually MOVE the selected file from the FileListBox into the target folder in the Directory ListBox and then refresh both components?

Thanks,
mjbcomp
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

kretzschmarCommented:
hi mjbcomp,

didn't i do this ?

movefileex should move the file
filelistbox1.update should refresh the filelistbox

well add DirectoryListBox1.Update to refresh the directory-listbox

maybe i misunderstand something, can you tell me, how filelistbox and dirctorylistbox are linked together ?

meikl
0
mjbcompAuthor Commented:
meikl,

Yes, I saw that in your code however it does not seem to work.

When I drag the file from the TFileListBox into a folder in the TDirectoryListBox it does nothing.

The cursor does change to the correct icon and I have both components setup for DragMode: Automatic.

I also have in the DirectoryListBox property of FileList: it is connected to my TFileListBox.

Any ideas?

MJBCOMP
0
kretzschmarCommented:
hmm,

maybe the api don't will work,
(happens now be me to, (another Win-OS))
i give you a workaround tomorrow

meikl
0
mjbcompAuthor Commented:
Can anybody help me with working code that actually deletes the files after they get moved. The example above does not move, copy or delete the files.

I need help!!!

mjbcomp
0
kretzschmarCommented:
hi mjbcomp,

sorry for delay
my mistake

the correction (i've swapped s-d), now it should work

procedure TForm1.DirectoryListBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  I,J : Integer;
  S,D : String;
begin
  I := DirectoryListBox1.ItemAtPos(Point(x,y),True);
  If I > -1 then
  Begin
    if I <> DirectoryListBox1.ItemIndex then
    Begin
      D := DirectoryListBox1.GetItemPath(I)+'\';
      S := DirectoryListBox1.GetItemPath(DirectoryListBox1.ItemIndex)+'\';

      For J := 0 to FileListBox1.Items.Count-1 do
      Begin
        if FileListBox1.Selected[J] then
         if Not MoveFileEx(PChar(S+FileListBox1.Items[J]),PChar(D+FileListBox1.Items[J]),
                       MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) then
           ShowMessage(IntToStr(GetLastError));
      end;
      FileListBox1.Update;
    end;
  end;
end;

meikl
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kretzschmarCommented:
hello?
0
mjbcompAuthor Commented:
Thank you so much for your help. This is a really good example. The only thing that this code could do that would make it absolutely perfect would be when you are dragging a file, if the file already exists pop up a dialog to let the user know instead of "automatically" overwriting the existing file.

Thanks,

mjbcomp
0
kretzschmarCommented:
well, here it comes

procedure TForm1.DirectoryListBox1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  I,J : Integer;
  S,D : String;
begin
  I := DirectoryListBox1.ItemAtPos(Point(x,y),True);
  If I > -1 then
  Begin
    if I <> DirectoryListBox1.ItemIndex then
    Begin
      D := DirectoryListBox1.GetItemPath(I)+'\';
      S := DirectoryListBox1.GetItemPath(DirectoryListBox1.ItemIndex)+'\';
      For J := 0 to FileListBox1.Items.Count-1 do
      Begin
        if FileListBox1.Selected[J] then
        begin
          if FileExists(D+FileListBox1.Items[J]) then
          begin
            If Application.MessageBox(PChar('Do you Want to Overwrite file '+D+FileListBox1.Items[J]),
                                      'File Exists', MB_YESNO+
                                      MB_ICONQUESTION+
                                      MB_DEFBUTTON1+
                                      MB_APPLMODAL) = ID_YES Then
              if Not MoveFileEx(PChar(S+FileListBox1.Items[J]),PChar(D+FileListBox1.Items[J]),
                            MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) then
                ShowMessage(IntToStr(GetLastError));
          end
          else
            if Not MoveFileEx(PChar(S+FileListBox1.Items[J]),PChar(D+FileListBox1.Items[J]),
                          MOVEFILE_COPY_ALLOWED or MOVEFILE_REPLACE_EXISTING) then
              ShowMessage(IntToStr(GetLastError));
        end;
      end;
      FileListBox1.Update;
    end;
  end;
end;

hope, its okay so, because i've just inserted it, and i guess, it could be a bit optimzed.

thanks for accepting my comment as answer
good luck again

meikl
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.