VB.Net move Items from ListBox to Textboxes

Posted on 2010-01-02
Last Modified: 2013-11-27
I have a listbox, populated with 5 items from a database. I wan't to drag items, one at a time, to 5 textboxes.

Here is my code for the source listbox:
Private Sub ListBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseDown
        ListBox2.DoDragDrop(ListBox2.SelectedValue.ToString, DragDropEffects.Move)
End Sub

Here is code for one of the destination textboxes:
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
  TextBox1.Text = e.Data.GetData(DataFormats.StringFormat)
 End Sub

The destination textboxes all have AllowDrop set to True.

If I try to move an item from the listbox, it gets copied instead (item appears in textbox but does not disappear from listbox)

Question by:LarryZ

    Author Comment

    ListBox2 is data bound with a System,Windoes.Forms.BindingSource to a SQL table. I'm thinking this is why I can't get the ListBox items to remove as drag and drop them on the textboxes.

    Hope this helps somewhat. I don't know how to handle it.

    Author Comment

    Evidently, it has nothing to do with BindingSource. I populated another ListBox manually. Drag and Copy works OK, But Drag and Move has two quirks. Only the first item dragged off ListBox appears in TextBox. After that, no more can be dragged. Also, the first item dragged still remains in ListBox.

    Microsoft's example for this is pretty bloated and difficult to utilize. Any easy ways to do this?

    Accepted Solution

    I figured it out. The trick is to place a statement in the target textbox's _DragDrop event, like this
    Now the dragged items are removed from the ListBox

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now