Drag and Drop Files
One common operation in Windows applications is dragging and dropping files onto applications. For example, you probably drag and drop files onto folders, or drag music files onto Windows Media player so that you can play them immediately.
You can modify the sample application so that users can drag an image file from Windows Explorer and drop it onto the PictureBox control in two steps:
- First, modify the DragEnter event (see Listing 2). The data type for a file that is dragged from the Windows Explorer is FileDrop, so the code in Listing 2 checks for that data type in the DragEnter event handler.
- Next, modify the DragDrop event as shown in Listing 3, so that you can manually open up the image file and display its content in the PictureBox control.
That's it! You can now drag an image file from Windows Explorer and drop it onto the PictureBox control and view its content. Note that in this particular implementation, users can drop one
or more files onto the PictureBox control. For multiple files, the example application displays each photo one by one with a two-second interval.
Implementing Drag and Drop for Special Controls
Most Windows Forms controls support the set (or a subset) of the events described in this article for drag and drop operations. However, what happens if the control you want to enable for drag and drop does
not support the standard list of events? A good example is the Windows Media Player ActiveX control. You might want to embed the Windows Media Player control in a Windows application so that users can simply drag and drop media files onto it to play. Unfortunately, the Windows Media Player ActiveX control by itself does not support events like
DragEnter and
DragDrop; therefore there's no easy way to implement drag and drop directly.
You can work around the problem by wrapping the ActiveX control using a User control. To do that, add a new User Control item (right-click the project name in Solution Explorer and select Add → New Item
→ select User Control) to the existing project. Name the file
MediaPlayer.vb.
Add the Windows Media Player to your Toolbox. To do that, right-click the Toolbox and select "Choose Items
." In the Choose Toolbox Items dialog box, click the COM Components tab, and check the Windows Media Player object (see
Figure 10). Click OK to add the Windows Media Player control to the Toolbox.
 | |
Figure 10. Adding the Windows Media Player ActiveX Control to the Toolbox. |
|
 | |
Figure 11. Populate the User Control with the Windows Media Player Control. |
|
|
Drag the Windows Media Player control from the Toolbox and drop it onto the
MediaPlayer.vb design surface (see
Figure 11).
The MediaPlayer control should now appear in the toolbox (see
Figure 12).
 | |
Figure 12. The MediaPlayer Control in the User Control. |
|
 | |
Figure 13. Adding the MediaPlayer Control to Form1. |
|
|
Drag and drop the MediaPlayer user control onto Form1 (see
Figure 13).
In the code view for
MediaPlayer.vb, add the following code:
Public Class MediaPlayer
Private _URL As String
Public Property URL() As String
Get
Return _URL
End Get
Set(ByVal value As String)
_URL = value
AxWindowsMediaPlayer1. _
URL = _URL
End Set
End Property
End Class
Essentially you expose the
URL property to let the user of this control set the URL of the media file to play.
Switch to the code-behind of Form1 and handle the
DragEnter event of the MediaPlayer user control as follows:
Private Sub MediaPlayer1_DragEnter( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) _
Handles MediaPlayer1.DragEnter
'---if the data to be dropped is
' an filedrop format---
If (e.Data.GetDataPresent( _
DataFormats.FileDrop)) Then
'---determine if this is a copy or move---
If (e.KeyState And CtrlMask) = CtrlMask Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
End If
End Sub
Finally, handle the
DragDrop event so that you can play the media file dropped by the user:
Private Sub MediaPlayer1_DragDrop( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DragEventArgs) _
Handles MediaPlayer1.DragDrop
If (e.Data.GetDataPresent( _
DataFormats.FileDrop)) Then
Dim files() As String
'---get all the file names---
files = e.Data.GetData( _
DataFormats.FileDrop)
If files.Length > 0 Then
'---load only the first file---
files(0) = UCase(files(0))
If files(0).EndsWith(."WMV") Then
'---get the media player to play the
' first file---
MediaPlayer1.URL = files(0)
End If
End If
End If
End Sub
 | |
Figure 14. Dragging and Dropping a Media File onto Form1. |
Note that because the user can drop multiple files onto the control, you will load only the first file using the MediaPlayer control.
Figure 14 shows the MediaPlayer control hosted in a Windows Form playing the file dropped onto it.
Dragging and Dropping Custom Objects
So far, you've seen how to use several of the various data types specified in the DataFormats class: Bitmap, CommaSeparatedValue, Dib, Dif, EnhancedMetafile, FileDrop, Html, Locale, MetafilePict, OemText, Palette, PenData, Riff, Rtf, Serializable, StringFormat, SymbolicLink, Text, Tiff, UnicodeText, and WaveAudio. But what happens if you want to drag and drop data of a type that's not listed? For example, you might want to drag an item from a ListView control. In this case, the
DragEnter event will look something like this:
If (e.Data.GetDataPresent _
("System.Windows.Forms. _
ListViewItem()")) Then
'---determine if this is a copy
'---or move---
If (e.KeyState And CtrlMask) =
CtrlMask Then
e.Effect =
DragDropEffects.Copy
Else
e.Effect =
DragDropEffects.Move
End If
The
MouseDown event handler will now look like this:
Control.DoDragDrop(New _
DataObject("System.Windows.Forms. _
ListViewItem()", Items), _
DragDropEffects.Move Or _
DragDropEffects.Copy)
As you can see, it isn't terribly difficult to implement drag and drop functionality in your Windows application. All you need is to understand the type of data you want to support and make the necessary provisions for dealing with that particular data type.