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

Knightlite_JamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kdwoodCommented:
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


0
Knightlite_JamesAuthor Commented:
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
 
0
kdwoodCommented:
James,

How are you adding the items to your listboxes initially?
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Knightlite_JamesAuthor Commented:
They are loaded from a database.  It is a bound control to a BindingSource.  So when the form loads the items are added automatically.
0
kdwoodCommented:
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
0
Knightlite_JamesAuthor Commented:
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
0
kdwoodCommented:
James,

Since your ListBoxes are databound, you will have to delete the item from the underlying datasource.

Take a look at this thread for examples of how to handle this:

http://www.experts-exchange.com/Programming/Languages/.NET/Q_20890851.html?sfQueryTermInfo=1+delet+item+listbox

Regards,

Keith
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Knightlite_JamesAuthor Commented:
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

0
Knightlite_JamesAuthor Commented:
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
0
kdwoodCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.