Link to home
Start Free TrialLog in
Avatar of Knightlite_James
Knightlite_James

asked on

How Do I Drag And Drop From A Listbox?

Hi,

I was wondering how one goes about dragging and dropping the selected text from one Listbox to another?

Can anyone help me with code.  I want to drag the selected text from Listbox1 to Listbox2


In Visual Basic 6.0 it used to be the code shown below, but it does not work with Visual Basic 2008

Any help would be appreciated.

Thanks

James


If Source = List1 then
   List2.AddItem List.Text
End If
 
' What is the code for Visual Basic 2008?

Open in new window

Avatar of kdwood
kdwood

Greetings James,

There are a handful of steps involved.

1.  On your listboxes from the design view, go to the properties window and look for the "AllowDrop" property.
     Make sure this is set to TRUE.

2.  On each listbox create a "MouseDown" event that will capture the listbox item that will be dragged.  I think you
     can use the "OnClick" event if it makes more sense for your project.  Here is the code that will go into the  
     MouseDown Event:

     ' First make sure they clicked an item

     If Me.YourListBox1.SelectedIndex <> -1 Then
         Dim dragData As String = Me.YourListBox1.SelectedItem
         Me.YourListBox1.DoDragDrop(dragData, DragDropEffects.Copy)
     End if

3. Create a "DragEnter" on each Listbox and use the following code:

       ' This code puts the + near the cursor and gives it the drag drop appearance.

       e.Effect = DragDropEffects.Copy

4.  Create a "DragDrop" event on each Listbox and use the following code:

       ' Add the dragged item to the list box

        Me.YourListBox2.Items.Add(e.Data.GetData(GetType(String)))

       ' Now remove the item from where it was dragged

        Me.YourListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndex)


That should do it for you.  Hope this helps.

Best regards,

Keith


Avatar of Knightlite_James

ASKER

Hi Keith,

Thank you for taking the time respond.

I am getting an error on the following code:

 Private Sub List1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles List1.MouseDown
        If Me.List1.SelectedIndex <> -1 Then

            Dim dragData As String = Me.List1.SelectedItem  'ERROR IS ON THIS LINE
            Me.List1.DoDragDrop(dragData, DragDropEffects.Copy)

        End If
    End Sub

--------
Error Details:

System.InvalidCastException was unhandled
  Message="Conversion from type 'DataRowView' to type 'String' is not valid."

When casting from a number, the value must be a number less than infinity.

-------

Any ideas how to fix this?

James
 
James,

How are you adding the items to your listboxes initially?
They are loaded from a database.  It is a bound control to a BindingSource.  So when the form loads the items are added automatically.
Ok, in that case, try this:

 Private Sub List1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles List1.MouseDown
        If Me.List1.SelectedIndex <> -1 Then

            Dim dragData As String = Me.List1.Text   ' Notice I'm using the .Text instead of selected item
            Me.List1.DoDragDrop(dragData, DragDropEffects.Copy)

        End If
    End Sub
Hi Keith,

That code worked great.  The only thing I can't get to work is removing the item from List1

Where does this code go?

Me.YourListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndex)

James
ASKER CERTIFIED SOLUTION
Avatar of kdwood
kdwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Keith,

I changed things around a little, so the List1 was not bound.  I just loaded the data in with code, so everything works now.

Thanks so much for you help.  I really appreciate you taking the time to help.  I write study software for firefighters EMTs, and Paramedics, and I am porting my applications from Visual Basic 6.0 to Visual Basic 2008, and having a little trouble adjusting.  You made my life a whole lot easier.

Thanks again.

James

Hi Keith,

I changed things around a little, so the List1 was not bound.  I just loaded the data in with code, so everything works now.

Thanks so much for you help.  I really appreciate you taking the time to help.  I write study software for firefighters EMTs, and Paramedics, and I am porting my applications from Visual Basic 6.0 to Visual Basic 2008, and having a little trouble adjusting.  You made my life a whole lot easier.

Thanks again.

James
Glad I could help.  I'll be happy to give you whatever help I can.  Sounds like it's for a great cause!!!!

Regards,

Keith