[RESOLVED] Drag & drop to Specific Listview folder
So I am trying to only allow drag & drop to a specific folder in my listview. The problem I am having is that with the current code below is that you start the drag to a folder not allowed it gives the denied symbol, but as you drag it down over the allowed folder it shows the allowed symbol yet if you pass the folder and go into some blank space I get an error "Object reference not set to an instance of an object." even though I have not dropped anything, yet it still shows the allowed sign even though the targetpoint is not over any listview item. why does it still allow the item to be dropped even though there is no listview folder underneath it? And how do I stop from getting the exception error?
Code:
Private Sub tvFolders_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles tvFolders.DragOver
' Retrieve the client coordinates of the mouse position.
Dim targetPoint As Point = tvFolders.PointToClient(New Point(e.X, e.Y))
Try
' Select the node at the mouse position.
Dim Selected As TreeNode = tvFolders.GetNodeAt(targetPoint)
'See if the targetNode is currently selected,
'if so no need to validate again
' If Selected.Text.Length > 0 Then
If Not (tvFolders.SelectedNode Is Selected) Then
'Select the node currently under the cursor
tvFolders.SelectedNode = Selected
Debug.WriteLine(tvFolders.SelectedNode.Text)
If My.Settings.Favorites.Contains(tvFolders.SelectedNode.Text) Then
'TreeNode found allow move effect
e.Effect = DragDropEffects.Copy
Else
'No TreeNode found, prevent move
e.Effect = DragDropEffects.None
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Re: Drag & drop to Specific Listview folder
I would assume that if there is no node at the specified point, the .GetNodeAt method will return Nothing/Null.
As such, you should ensure that the Selected variable doesn't contain Nothing before trying to use it, eg:
Code:
If Selected IsNot Nothing Then
Re: Drag & drop to Specific Listview folder
Thanks Si_the_geek,
I did try a variation of that
If Selected.Text.Length > 0 Then
And still got the error. But trying yours doesn't give me an error until I release the left mouse button while going into the blank space in the listview control. I do have a DragLeave event to go back to the original listview folder they started from. Which doesn't fire once I let the button go. I guess I need to find a way to gracefully quit if in a denied area
Re: Drag & drop to Specific Listview folder
You need to test if the mousepointer is over a selectable node in your dragdrop event
Re: Drag & drop to Specific Listview folder
I think I figured it out. So once i realized that GetNodeAt retuned nothing/null I changed my checks to check for nothing instead of text length. I can then drag and let go in white space and not error anymore.
thank you sir.
Re: Drag & drop to Specific Listview folder
Quote:
Originally Posted by
.paul.
You need to test if the mousepointer is over a selectable node in your dragdrop event
Yes I was, just not the correct way. But thank you