Results 1 to 2 of 2

Thread: Drag and Drop Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    1

    Question Drag and Drop Problem

    Hello all.

    I have a portion of code that allows the user to drag items from one ListBox control and drop them onto another ListBox control. Up until a few days ago, I was using Visual Basic 2003 to develop this project. At that time, the drag and drop code was working. After purchasing and installing Visual Basic 2005, I converted my project to the new format. Now that I have converted, the drag and drop portion of my code no longer works.

    Here is a description of the problem:

    I click an item in the source ListBox and begin a drag. The mouse cursor becomes a circle with a slash through it, indicating an invalid drop target. The problem is that whenever I drag the cursor over the destination list box, the cursor never changes to indicate a valid drag target. The AllowDrop property is set in the destination list box.

    Here is the code I have for drag and drop concerning the source and destination list boxes. StockItemBox is the source ListBox, StockItemCategoryBox is the destination ListBox.

    Code:
    Private Sub StockItemBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles StockItemBox.MouseDown
    
            If StockItemBox.SelectedIndex < 0 Then Exit Sub
    
            Dim DragText As String = StockItemBox.Items(StockItemBox.SelectedIndex)
    
            StockItemBox.DoDragDrop(DragText, DragDropEffects.Move)
    
        End Sub
    Code:
    Private Sub StockItemCategoryBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles StockItemCategoryBox.DragEnter
    
            If (e.Data.GetDataPresent(DataFormats.Text)) Then
                ' Display the copy cursor.
                e.Effect = DragDropEffects.Copy
            Else
                ' Display the no-drop cursor.
                e.Effect = DragDropEffects.None
            End If
    
        End Sub
    Code:
    Private Sub StockItemCategoryBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles StockItemCategoryBox.DragDrop
    
            'extract the dropped text
            Dim TextDropped As String = e.Data.GetData(DataFormats.Text).ToString
    
            MsgBox(TextDropped)
    
        End Sub
    The StockItemCategoryBox_DragDrop method is never called.

    I'm not sure if it makes a difference, but the form that these controls are located on is displayed using a ShowDialog call.

    Anyone have any suggestions? Is there something I am missing here? It is weird that the code worked fine with the 2003 project but not the newly converted project.
    Last edited by mob4l; Oct 4th, 2007 at 03:20 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Drag and Drop Problem

    What is the second argument of the DoDragDrop method called? It's called 'allowedEffects'. What effects are you saying are allowed? You'r saying that Move and Move only is allowed. Then when you enter the drop target you're trying to set the effect to Copy. Copy is not Move so it's not allowed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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