• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

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
0
mjbcomp
Asked:
mjbcomp
  • 6
  • 5
1 Solution
 
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now