Sinisterly
[VB6, VB.Net] [Tutorial] How to Drag & Drop in your Form - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework)
+--- Thread: [VB6, VB.Net] [Tutorial] How to Drag & Drop in your Form (/Thread-VB6-VB-Net-Tutorial-How-to-Drag-Drop-in-your-Form)



[VB6, VB.Net] [Tutorial] How to Drag & Drop in your Form - Coder-san - 01-27-2011

[VB6, VB.Net] [Tutorial] How to Drag & Drop in your Form

Hello and welcome to this short Tutorial about how to drag and drop from Windows to your Form. Smile


VB6

First we need to set the OLEDropMode property to Manual - 1
Now we will use the OLEDragDrop property of the Control.

In our Example we are using a ListBox names List1

Code:
Private Sub List1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single) List1.AddItem Data.Files(1) End Sub



VB.Net

First we need to set the AllowDrop property of a Control to True.
Now we have to use 3 events of the Control for a successful Drag & Drop.
  • DragDrop
  • DragEnter
  • MouseDown

In our Example we are using a ListBox named lstProxy

Code:
Private Sub lstProxy_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstProxy.DragDrop Dim theFile As String = (CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString) MsgBox(theFile) End Sub Private Sub lstProxy_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstProxy.DragEnter e.Effect = DragDropEffects.Copy End Sub Private Sub lstProxy_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstProxy.MouseDown lstProxy.DoDragDrop(lstProxy, DragDropEffects.Copy) End Sub



Now just run your application and drag and drop a file in your Control to test the usage.
Have a great day. Smile