SUMMARY
This step-by-step article
describes how to provide file drag-and-drop functionality in a Microsoft Visual
C++ .NET application. A
ListBox control is used as the
destination of the file drag-and-drop procedure.
Back to the topRequirements
The
following list outlines the recommended hardware, software, network
infrastructure, and service packs that you need:
- Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
- Microsoft .NET Framework 1.1
This
article assumes that you are familiar with the following topics:
- Windows Forms ListBox control
- Windows Forms event handling
Back to the topSteps to build the
sample
The
ListBox control provides two drag-and-drop events:
DragEnter and
DragDrop. The
DragEnter event occurs when you drag an object in the bounds of the
control and is used to determine whether the object that is being dragged is
one that you can drop on the control. You use the
DragEnter event when you drag a file or files to the control.
The Windows form displays the appropriate icon when the object is dragged over the
control, depending on the object that is being dragged. The
DragDrop event occurs when you release the object that is being dragged on the control. You use the
DragDrop event to retrieve the object. You use the
Data object to retrieve the data.
The
GetData method of the
Data object 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
operations on the files. For example, you can
use classes in the
System::IO namespace to open and read the files, to move the files, or to copy the
files to a new location. In this example, you add the full path of the
files that are dragged to the
ListBox control.
To provide file drag-and-drop functionality in
a Visual C++ .NET or Visual C++ 2005 application, follow these steps:
- Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Click Visual
C++ Projects under Project Types, and then click Windows
Forms Application (.NET) under Templates.
Note In Visual Studio 2005, Visual
C++ Projects is changed to Visual
C++ and Windows
Forms Application (.NET) is changed to Windows
Forms Application. - In the Name box, type
DragDrop, and then click OK.
By default, the Form1 form is created, and is opened in
Design mode.
- Add a ListBox
control to Form1.
- Right-click listBox1, and then click
Properties.
- Change the AllowDrop property to
True.
- Click the Events button.
- Double-click the DragEnter event to add a
listBox1_DragEnter event handler to the code
window.
- Add the following code in the
listBox1_DragEnter event handler.
if(e->Data->GetDataPresent(DataFormats::FileDrop))
e->Effect = DragDropEffects::All;
else
e->Effect = DragDropEffects::None;
- On the View menu, click
Designer to switch to Design mode.
- Right-click listBox1, and then click
Properties.
- Click the Events
button.
- Double-click the DragDrop event to add a
listBox1_DragDrop event handler to the code window.
- Add the following code in the
listBox1_DragDrop event handler.
String *s[] = (String *[]) e->Data->GetData(DataFormats::FileDrop, false);
int i;
for(i = 0; i < s->Length; i++)
listBox1->Items->Add(s[i]);
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in
Visual C++ 2005 to successfully compile the previous code sample.
To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the
Common Language Runtime support project setting in the right pane, click Apply, and then
click OK.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - Press the CTRL+SHIFT+S key combination to save the
project.
- Press the CTRL+SHIFT+B key combination to build the
solution.
- Press the CTRL+F5 key combination to run the
project.
- Drag one or more files from the desktop or from another
folder to the ListBox control. Notice that the full path of the files is added to the ListBox control.
Back to the
top