.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Drag and Drop from ListBox to TextBox the Simple Way!

Posted By:Hefin Dsouza       Posted Date: August 24, 2009    Points: 10    Category: Windows Form Controls    URL: http://www.dotnetspark.com  
 

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.

Regards
Hefin Dsouza


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend