#696 – A Drop Target Can Receive Data from Other Applications

When you designate an element in your user interface as a drop target for a drag-and-drop operation, it can receive data that is dragged from other elements in your application, provided that those elements initiate the drag-and-drop operation.  But a drop target element in your application can also serve as a target for a drag source in another application.

You can set the AllowDrop property to true for an element and then add code for the element’s Drop event, where you call IDataObject.GetData to get the dragged data.

In the example below, we’ve made a ListBox the drop target.  We can then drag some text onto it from Microsoft Word.

        private void lbDrop(object sender, DragEventArgs e)
        {
            string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);

            ListBox lbox = e.Source as ListBox;
            lbox.Items.Add(draggedText);
        }

Advertisements