Microsoft KB Archive/815667

= How to provide file drag-and-drop functionality in a Visual C++ .NET application or in a Visual C++ 2005 application =

Article ID: 815667

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1

-







For a Microsoft Visual Basic .NET version of this article, see 306969.



For a Microsoft Visual C# .NET version of this article, see 307966.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::ComponentModel
 * System::Collections
 * System::Windows::Forms
 * System::Data
 * System::Drawing

IN THIS TASK

 * SUMMARY
 * Requirements
 * Steps to build the sample
 * REFERENCES



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 top

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

Steps 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.</li> Change the AllowDrop property to True.</li> Click the Events button.</li> Double-click the DragEnter event to add a listBox1_DragEnter event handler to the code window.</li>  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; </li> On the View menu, click Designer to switch to Design mode.</li> Right-click listBox1, and then click Properties.</li> Click the Events button.</li> Double-click the DragDrop event to add a listBox1_DragDrop event handler to the code window.</li>  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: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> 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.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Press the CTRL+SHIFT+S key combination to save the project.</li> Press the CTRL+SHIFT+B key combination to build the solution.</li> Press the CTRL+F5 key combination to run the project.</li> <li>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.</li></ol>

Back to the top

<div class="references_section">