[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1442
  • Last Modified:

VB.Net move Items from ListBox to Textboxes

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
        TextBox1.AppendText(e.Data.GetData(DataFormats.StringFormat))
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)


0
LarryZ
Asked:
LarryZ
  • 3
1 Solution
 
LarryZAuthor Commented:
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.
0
 
LarryZAuthor Commented:
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?
0
 
LarryZAuthor Commented:
I figured it out. The trick is to place a statement in the target textbox's _DragDrop event, like this
        ListBox1.Items.Remove(ListBox1.SelectedItem)
Now the dragged items are removed from the ListBox
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now