Quantcast
Channel: AuthorCode » ListBox control
Viewing all articles
Browse latest Browse all 9

Drag and drop operation between two Listbox using vb.net

$
0
0

 
The following example demonstrates how to perform a drag and drop operation between two listbox controls in vb.net

Example

this example requires one windows form that contains Two listbox controls named ListBox1 and ListBox2, Use the toolbox to add two listbox controls to Form1. Change the AllowDrop property of both ListBox1 and ListBox2 to True in the Properties window.

Generate method handler for Load event of Form1 and use this code for binding items to ListBox1 :

  1. Private Sub Form1_Load(ByVal sender As System.Object, _
  2.                            ByVal e As System.EventArgs) Handles MyBase.Load
  3.         ListBox1.Items.Add("NewDelhi")
  4.         ListBox1.Items.Add("Kolkata")
  5.         ListBox1.Items.Add("Londan")
  6.         ListBox1.Items.Add("Sydney")
  7.         ListBox1.Items.Add("NewYork")
  8.         ListBox1.Items.Add("Dubai")
  9. End Sub

Generate method handler for MouseDown event of ListBox1 and use this code :

  1.     Private Sub ListBox1_MouseDown(ByVal sender As System.Object, _
  2.                      ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
  3.         If ListBox1.Items.Count = 0 Then
  4.             Return
  5.         End If
  6.         Dim index As Integer = ListBox1.IndexFromPoint(e.X, e.Y)
  7.         Dim sourceStr As String = ListBox1.Items(index).ToString()
  8.         Dim objDragDropEff As DragDropEffects = DoDragDrop(sourceStr, DragDropEffects.All)
  9.         If objDragDropEff = DragDropEffects.All Then
  10.             ListBox1.Items.RemoveAt(ListBox1.IndexFromPoint(e.X, e.Y))
  11.         End If
  12.     End Sub

Generate method handler for DragEnter event of ListBox2 and use this code :

  1.     Private Sub ListBox2_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragEnter
  2.         e.Effect = DragDropEffects.All
  3.     End Sub

Generate method handler for DragDrop event of ListBox2 and use this code :

  1.     Private Sub ListBox2_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox2.DragDrop
  2.         Dim str As String = CStr(e.Data.GetData(DataFormats.StringFormat))
  3.         ListBox2.Items.Add(str)
  4.     End Sub

Viewing all articles
Browse latest Browse all 9

Trending Articles