XDrag and Drop

L

Lou

need to implement drag and drop facility in a listview.
For example i have 10 listitems in my listview
i need to drag and change the positions of listitems up and down
-Lou
 
S

Shane

This code assumes that you have a listview named "lvw" and a button
called "btnDelete"
Basically, you get a list of the selected items, delete the items, then
insert the selected items in the new position.

Private Sub lvw_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles lvw.DragDrop
Try
Dim i As Integer
Dim l_Point As Point = lvw.PointToClient(New Point(e.X,
e.Y))
Dim l_Item As ListViewItem = lvw.GetItemAt(l_Point.X,
l_Point.Y)
Dim l_Items() As ListViewItem =
e.Data.GetData("System.Windows.Forms.ListViewItem()")
Dim l_DropIndex As Integer = l_Item.Index

btnDelete_Click(sender, New System.EventArgs)
For Each l_Item In l_Items
lvw.Items.Insert(l_DropIndex + i, l_Items(i))
i = i + 1
Next
Catch ex As Exception
End Try
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnDelete.Click
Dim l_Item As ListViewItem
Dim l_SelectedCols As ListView.SelectedListViewItemCollection
Dim i As Integer

l_SelectedCols = lvw.SelectedItems
lvw.BeginUpdate()
For i = l_SelectedCols.Count To 1 Step -1
l_Item = l_SelectedCols(i - 1)
l_Item.Remove()
Next
lvw.EndUpdate()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top