The following example demonstrates how to perform a drag and drop operation between Listbox and Textbox controls in vb.net
this example requires one windows form that contains one listbox controls named ListBox1 and one textbox named TextBox1, Use the toolbox to add these controls to Form1. Change the AllowDrop property of both ListBox1 and TextBox1 to True in the Properties window.
Generate method handler for Load event of Form1 and use this code for binding items to ListBox1 :
- Private Sub Listbox1_Mousedown(ByVal Sender As Object, _
- ByVal e As System.Windows.Forms.MouseEventArgs) Handles Listbox1.Mousedown
- Dim Selecteditems As String
- For i As Integer = 0 To Listbox1.Selecteditems.Count – 1
- Selecteditems = Selecteditems & "," & Listbox1.Selecteditems.Item(i)
- Next
- Listbox1.Dodragdrop(Selecteditems, DragDropEffects.Copy Or DragDropEffects.Move)
- End Sub
- Private Sub TextBox1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
- e.Effect = DragDropEffects.Copy
- End Sub
- Private Sub TextBox1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
- TextBox1.Text = e.Data.GetData(DataFormats.Text).ToString
- End Sub