Hi,
Before we begin, there a few points to note :
1) The AllowDrop property of the control must be set to True
2) If more than one files are being collectively being dropped on a control, you can determine if you want to use only one file or use the entire collection.
3) You can also determine the type of file/data being dropped on a control and whether you want to allow it.
There are three events associated with a common drag-drop operation.
DragEnter : When the mouse enters the control carrying the data/files to be dropped.
Please note that
Dataformats Class given in the code below has some predefined formats.
For instance :
- If files are being dropped then use : DataFormats.FileDrop
- If Image is being dropped then use : DataFormats.Bitmap
- If a text string is being dropped then use : DataFormats.Text
'Check if the type of Drop is a
FILECODE
Private Sub YourControlName_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles YourControlName.DragEnter
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
'---change the border style of the control---
yourcontrol.borderstyle = BorderStyle.FixedSingle
End If
End Sub
'Check if the type of Drop is a
TEXTCODE
Private Sub YourControlName_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles YourControlName.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) 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
'---change the border style of the control---
yourcontrol.borderstyle = BorderStyle.FixedSingle
End If
End Sub
DragLeave : When the mouse leaves the control
all you need to do here is set the
BorderStyle property back to
NoneCODE
Private Sub YourControlName_DragLeaveByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles YourControlName.DragLeave
yourcontrol.borderstyle = none
End Sub
DragDrop : This is where the actual process of drop takes place.
The example below illustrates the use of File Drag-Drop
'Use this code in the Drag-Drop event of the Control
CODE
Private Sub YourControlName_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles YourControlName.DragDrop
Dim theFiles() As String = CType(e.Data.GetData("FileDrop", True), String())
For Each theFile As String In theFiles
Next
End Sub
The example below illustrates the use of Text Drag-Drop
'Use this code in the Drag-Drop event of the Control
CODE
Private Sub YourControlName_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles YourControlName.DragDrop
Dim DATaa = e.Data.GetData(DataFormats.Text).ToString()
End Sub
Hope this helps..
kasbaba