Create a Drag and Drop Program in VB.NET

Drag and Drop is a very common feature in VB.NET. It is when you grab an object and drag it to a different location. In VB.NET, drag and drop is part of the standard, and any controls can be draggable. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add only one TextBox named TextBox1 and ListBox named ListBox1. Change the AllowDrop property of the target to True.You must design your interface like this: design 3. Now put this code for your code module.
  1. Public Class Form1
  2.  
  3. Private Sub TextBox1_MouseDown( ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
  4.     TextBox1.DoDragDrop(TextBox1.Text,DragDropEffects.Copy)
  5.     End Sub
  6.  
  7.     Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter
  8.         e.Effect = DragDropEffects.Copy
  9.     End Sub
  10.  
  11.     Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
  12.         ListBox1.Items.Add(e.Data.GetData(DataFormats.Text).ToString())
  13.         TextBox1.Clear()
  14.     End Sub
  15. End Class

Explanation:

We use first the MouseDown Event of our TextBox1. MouseDown Event occurs when the mouse pointer is over the control and a mouse button is pressed. Then the textbox property call the DoDragDrop method. This method has two parameters. The first is the data that will be dragged (this refers to the contents of the Textbox because we will drag this into the ListBox) and the second sets the value of an enumeration, DragDropEffects, that tells the source what kind of drag and drop will be done. We also call the event DragEnter for ListBox. DragEnter event occurs when an object is dragged into the control's bounds. Usually in the DragEnter event to set the target Effect property of the e parameter. Next, we also call the event DragDrop for ListBox. DragDrop occurs when a drag-and-drop operation is completed. This will complete the drag and drop operation in the DragDrop event of the target.

Output:

output output Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment