ToshNeox
ToshNeox

Reputation: 117

VB.NET DragAndDrop not working on controls

I'm trying to make a program which uses drag & drop functionality, however, it only works when I drop something onto the form, not the controls. If I do try on the controls, I just get the 'Unavailable' cursor.

The AllowDrop property is set in the properties bar, and I also set it when the form loads. I have no idea why I still can't drop things on; has anyone had this problem before?

Current code:

Public Class Main

Private Sub Main_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    If e.Data.GetDataPresent("FileDrop", True) = True Then

        Dim Files() As String
        Dim i As Integer

        Files = e.Data.GetData(DataFormats.FileDrop)

        For i = 0 To Files.Length - 1
            FileList.Items.Add(Files(i))
        Next

    End If
End Sub

Private Sub Main_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If
End Sub

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles     Me.Load
    Me.AllowDrop = True
    FilePath.AllowDrop = True
    FileList.AllowDrop = True
End Sub
End Class

Upvotes: 2

Views: 9369

Answers (3)

Karl Sangree
Karl Sangree

Reputation: 111

I know this is an old thread, but I'll post what I found in case someone else is out there pulling their hair out like I've been doing for the last 2 hours.

Dropping a file onto a form (or a form's component) doesn't work if VS is started in Administrator mode.

Grrrrrrrr.....

Upvotes: 4

Kratz
Kratz

Reputation: 4330

Change

Private Sub Main_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If
End Sub

to

Private Sub Main_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FilePath.DragEnter, FileList.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.All
    End If
End Sub

You should be handling the DragEnter event for both controls instead of for the form itself.

Upvotes: 1

Nico Schertler
Nico Schertler

Reputation: 32597

You have to specify the DragDrop-Effect for each control in the according event. So besides setting AllowDrop to True, you have to add an event handler. For example like this:

Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles     Me.Load 
    Me.AllowDrop = True 
    FilePath.AllowDrop = True 
    AddHandler FilePath.DragEnter, AddressOf Main_DragEnter
    FileList.AllowDrop = True 
    AddHandler FileList.DragEnter, AddressOf Main_DragEnter
End Sub

Maybe you then should choose a name that fits better for the method Main_DragEnter.

Upvotes: 3

Related Questions