Provide file drag-and-drop functionality in a Visual C# application
This article shows how to provide file drag-and-drop functionality in a Visual C# application.
Original product version: Visual C++
Original KB number: 307966
Summary
The step-by-step procedure that is outlined in this article demonstrates how to provide file drag-and-drop functionality in a Visual C# application. A ListBox
control is used as the destination of the file drag-and-drop procedure.
Requirements
This list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Visual C#.
This article assumes that you are familiar with the following topics:
Windows Forms ListBox control
Windows Forms event handling
Steps to Build the Sample
The ListBox control provides two drag-and-drop events that you need to handle: DragEnter
and DragDrop
. The DragEnter
event occurs when you drag an object within the bounds of the control and is used to determine whether the object that is being dragged is one that you want to allow to be dropped on the control. You handle this event for cases in which a file or files are dragged to the control. This allows the appropriate icon to be displayed when the object is dragged over the control, depending on the object that is being dragged. The DragDrop
event occurs when the object that is being dragged has been released on the control. You handle this event to retrieve the object. The Data object is used to retrieve the data.
The Data object's GetData
method returns an array of strings that contain the full path names of the files that were dragged to the ListBox
control. You can use this file path information to perform whatever operations are needed on the files. For example, you can use classes in the System.IO
namespace to open and read the files, move the files, or copy the files to a new location. In this example, you just add the full path to the files that are dragged to the ListBox
control.
To provide file drag-and-drop functionality in a Visual C# application, follow these steps:
Create a new Windows Forms application in Visual C# .NET or Visual C# 2005. Form1 is created by default.
Use the toolbox to add a
ListBox
control to Form1.In the Properties window, change the
AllowDrop
property of theListBox
control to True to allow objects to be dragged onto the control.In Solution Explorer, right-click Form1, and then click View Code.
To handle the
DragEnter
event, add the following method below the code section that the Windows Form Designer generates in theForm1
class:private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.All; else e.Effect = DragDropEffects.None; }
To handle the
DragDrop
event, add the following method to theForm1
class immediately following the method that you added in step 5:private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { string[] s = (string[]) e.Data.GetData(DataFormats.FileDrop, false); int i; for(i = 0; i < s.Length; i++) listBox1.Items.Add(s[i]); }
To associate the two event handlers with the control events, add the following code in the
Form1
constructor after the call toInitializeComponent
:this.listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.listBox1_DragDrop); this.listBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listBox1_DragEnter);
Build and run the project.
Drag one or more files from either the desktop or another folder to the ListBox control. The full path of the files is added to the
ListBox
control.
References
For more information, see the following Web sites: