?
Solved

How Do I Drag And Drop From A Listbox?

Posted on 2008-11-14
10
Medium Priority
?
887 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:Knightlite_James
  • 5
  • 5
10 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 22960954
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
 

Author Comment

by:Knightlite_James
ID: 22961299
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
 
LVL 10

Expert Comment

by:kdwood
ID: 22961356
James,

How are you adding the items to your listboxes initially?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:Knightlite_James
ID: 22961453
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
 
LVL 10

Expert Comment

by:kdwood
ID: 22962109
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
 

Author Comment

by:Knightlite_James
ID: 22962324
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
 
LVL 10

Accepted Solution

by:
kdwood earned 2000 total points
ID: 22963362
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
 

Author Comment

by:Knightlite_James
ID: 22964038
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
 

Author Closing Comment

by:Knightlite_James
ID: 31516816
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
 
LVL 10

Expert Comment

by:kdwood
ID: 22964172
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are like me and like multiple layers of protection, read on!
Stellar Exchange Toolkit: this 5 in 1 toolkit comes loaded with mega-software tool. Here’s an introduction to tools’ usage and advantages:
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question