RAD Studio |  |

Dropping Items
See Also
Collapse All

If a control indicates that it can accept a dragged item, it needs to handle the item should it be dropped. To handle dropped items, attach an event handler to the OnDragDrop event of the control accepting the drop. Like the drag-over event, the drag-and-drop event indicates the source of the dragged item and the coordinates of the mouse cursor over the accepting control. The latter parameter allows you to monitor the path an item takes while being dragged; you might, for example, want to use this information to change the color of components if an item is dropped.

In the following VCL example, a directory tree view, accepting items dragged from a file list box, responds by moving files to the directory on which they are dropped.

Copy Code

    procedure TFMForm.DirectoryOutline1DragDrop(Sender, Source: TObject; X,
      Y: Integer);
    begin
      if Source is TFileListBox then
        with DirectoryOutline1 do
          ConfirmChange('Move', FileListBox1.FileName, Items[GetItem(X, Y)].FullPath);
    end;

Copy Code

    void __fastcall TForm1::TreeView1DragDrop(TObject *Sender, TObject *Source,
      if (Source->InheritsFrom(__classid(TFileListBox)))
      {
        TTreeNode *pNode = TreeView1->GetNodeAt(X,Y); // pNode is drop target
        AnsiString NewFile = pNode->Text + AnsiString("//") +
          ExtractFileName(FileListBox1->FileName); // build file name for drop target
        MoveFileEx(FileListBox1->FileName.c_str(), NewFile.c_str(),
          MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED); // move the file
      }
    }

See Also

TWinControl

Implementing Drag and Drop in Controls

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!