Results 1 to 6 of 6

Thread: [RESOLVED] Drag & drop to Specific Listview folder

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Drag & drop to Specific Listview folder

    You need to test if the mousepointer is over a selectable node in your dragdrop event

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Drag & drop to Specific Listview folder

    Quote Originally Posted by .paul. View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width