Well the title pretty well describes my problem. Here is a little bit more detailed description of my problem:
I am building an application with a TabControl, which I populate at execution time with TabPages. In my first version of the code, these TabPages were filled with a children ListView. The ListView was also created in code, using AddHandlers to link it to the DragEnter and DragDrop routines. Everything worked very well…
Now as I need some other controls on every TabPage, instead of creating every single control in code, I have created a UserControl containing a ListView and a few Buttons, which I instantiate for every new TabPage.
The problem now is that the DragDrop event is not raised anymore
DragEnterevent –> raised OKDragOverevent –> raised OKDragLeaveevent –> raised OKDragDropevent –> not raised !!
This problem is driving me crazy: any thoughts on what I could be missing?
I don’t think the code will be of any help, but to answer the first comment:
First Version: controls created in code (works):
Dim NewTab As New TabPage(TextBox1.Text)
Dim NewListView As New ListView()
Dim NewImageList As New ImageList()
'Organise les nouveaux contrôles
NewListView.Parent = NewTab
NewListView.Dock = DockStyle.Fill
NewListView.View = View.LargeIcon
NewListView.LargeImageList = NewImageList
NewListView.AllowDrop = True
AddHandler NewListView.DoubleClick, AddressOf ListViewItem_DblClick
AddHandler NewListView.DragEnter, AddressOf ListViewItem_DragEnter
AddHandler NewListView.DragDrop, AddressOf ListViewItem_DragDrop
TabControl1.TabPages.Add(NewTab)
TabControl1.SelectTab(NewTab)
NewImageList.ImageSize = New Size(100, 100)
NewImageList.ColorDepth = ColorDepth.Depth24Bit
TabControl1.Refresh()
Second version: Usercontrol (does not work):
First, imagine a user control TabAnnoncesContent, which contains a AnnoncesListView, and an AnnoncesImageList
Dim NewTab As New TabPage(TextBox1.Text)
Dim NewTabContent As New TabAnnoncesContent()
NewTabContent.Parent = NewTab
NewTabContent.Dock = DockStyle.Fill
TabControl1.TabPages.Add(NewTab)
TabControl1.SelectTab(NewTab)
TabControl1.Refresh()
Then inside my TabAnnoncesContent class code I have:
Private Sub AnnoncesListView_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AnnoncesListView.DragDrop
Dim selection As ListViewItem = sender.HitTest(sender.PointToClient(New Point(e.X, e.Y))).Item
If (selection IsNot Nothing) Then
MsgBox("D&D received -> " & e.Data.GetData(DataFormats.Text))
End If
End Sub
Private Sub AnnoncesListView_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles AnnoncesListView.DragEnter
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Link
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
The AnnonceListView_DragEnter routines is executed when I enter the ListView with text, but then when I release the mouse, the AnnonceListView_DragDrop event is never raised.
Not using Option Strict On in your code is a Visual Basic convenience. You’ll get runtime errors instead of compile errors when you get the code wrong. That’s doesn’t work out well in drag+drop code though, the events are called with a back-stop that swallows all exceptions. The only diagnostic you’ll get is a first chance exception notification in the Output window. Very easy to miss of course.
Anyhoo, Debug + Exceptions, Thrown checkbox to get the debugger to stop on the exception. And Option Strict On at the top of your source code file to catch these kind of mistakes early. That does however require a different code writing style, more akin to C#.
Btw, you should also implement the DragOver event. So you can call the ListView’s HitTest() method and check that the user is actually hovering over a ListViewItem.