,
Share with your friends 

Drag and Drop from ListBox to TextBox the Simple Way!

6 ratings Views 568 
Author: Hefin (Hefin Dsouza)  View Profile |  View other solutions by this author

Question / Problem


How do I Implement Drag and Drop from ListBox to Textbox using VB.NET Winforms.If I have to Drag and Drop Items from a List of Items to a TextBox or To Another ListBox

Solution

This simple code will allow users to implement drag and drop features in their applications

1) Set The AllowDrop property of the destination control to True
Destination Control is the control to where you will drag drop an Item
2) On the MouseDown Event of the Destination Control add

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System
.Windows.Forms.MouseEventArgs) Handles sourceControl.MouseDown
sourceControl.DoDragDrop(sourceControl.SelectedItem, DragDropEffects.Copy)
End Sub

*Source Control Is the Control from which you will drag.

3)In the DragEnter event handler of the destination control add

Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles destControl.DragEnter
e.Effect = DragDropEffects.Copy
End Sub


4)In the DragDrop event of the destination control add

Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles destControl.DragDrop
destControl.Items.Add(e.Data.GetData("System.String"))
End Sub
5)For Droping Item in a textBox use the Following Code
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As  System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
TextBox1.Text &= " " & e.Data.GetData("System.String")
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
     e.Effect = DragDropEffects.Copy
End Sub

This Finishes an Application that drag's data from one listbox and drops it into another.
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As  System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
TextBox1.Text &= " " & e.Data.GetData("System.String")
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
     e.Effect = DragDropEffects.Copy
End Sub

This Finishes an Application that drag's data from one listbox and drops it into another.
Regards
Hefin Dsouza

Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System
.Windows.Forms.MouseEventArgs) Handles sourceControl.MouseDown
sourceControl.DoDragDrop(sourceControl.SelectedItem, DragDropEffects.Copy)
End Sub

*Source Control Is the Control from which you will drag.

3)In the DragEnter event handler of the destination control add

Private Sub ListBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles destControl.DragEnter
e.Effect = DragDropEffects.Copy
End Sub


4)In the DragDrop event of the destination control add

Private Sub ListBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles destControl.DragDrop
destControl.Items.Add(e.Data.GetData("System.String"))
End Sub
5)For Droping Item in a textBox use the Following Code
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As  System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
TextBox1.Text &= " " & e.Data.GetData("System.String")
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
     e.Effect = DragDropEffects.Copy
End Sub

This Finishes an Application that drag's data from one listbox and drops it into another.
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As  System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
TextBox1.Text &= " " & e.Data.GetData("System.String")
End Sub
Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
     e.Effect = DragDropEffects.Copy
End Sub

Applies to

.Net 2.0 Windows Forms ,C# 2.0

Rank It

Login to rank it

Report


Advertisement