Visual Basic - Listview ordenar items con arrastrar y soltar (vb.net)

Life is soft - evento anual de software empresarial
 
Vista:
sin imagen de perfil

Listview ordenar items con arrastrar y soltar (vb.net)

Publicado por Jorge (2 intervenciones) el 19/10/2022 10:05:25
Listview ordenar items con arrastrar y soltar (vb.net)

Busqué por horas un código en vb.net que me ayudara con algo que pensé sería fácil, acomodar los items de un listview con el mouse, pero por más que busque no logre encontrar algo que fuera lo suficientemente funcional, por esto me di a la tarea de publicar el código que logre hacer y que si funciona sin errores y además dejare el link con el código fuente.

https://mega.nz/folder/ntIA2BLB#PYJpHEN_bYWsv5XZ95SyUQ

spectaculargratefulfieldspaniel



Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Listview1.FullRowSelect = True
Listview1.View = View.Details
ListView1.MultiSelect = False
ListView1.AllowDrop = True 'Importante este true

For i = 1 To 5
With ListView1
.Font = New Font(New FontFamily("Arial Narrow"), 14, FontStyle.Regular)
With .Columns.Add("Columna " & i)
.Width = 180
End With

With .Items.Add("Item " & i)
.SubItems.Add("Item " & i & " SubItem 1")
.SubItems.Add("Item " & i & " SubItem 2")
.SubItems.Add("Item " & i & " SubItem 3")
.SubItems.Add("Item " & i & " SubItem 4")
End With
End With
Next
End Sub

Private Sub ListView1_ItemDrag(sender As Object, e As ItemDragEventArgs) Handles ListView1.ItemDrag
ListView1.DoDragDrop(ListView1.SelectedItems, DragDropEffects.Move)
ListView1.Items.Remove(e.Item)
End Sub

Private Sub ListView1_DragEnter(sender As Object, e As DragEventArgs) Handles ListView1.DragEnter
e.Effect = DragDropEffects.Move
End Sub

Private Sub ListView1_DragDrop(sender As Object, e As DragEventArgs) Handles ListView1.DragDrop
Dim pt As Point = ListView1.PointToClient(Cursor.Position)
Dim hit As ListViewHitTestInfo = ListView1.HitTest(pt)
Dim sel As Windows.Forms.ListView.SelectedListViewItemCollection
sel = e.Data.GetData(GetType(Windows.Forms.ListView.SelectedListViewItemCollection))
If hit.Item Is Nothing Then
For Each lvi As ListViewItem In sel
ListView1.Items.Add(lvi.Clone)
Next
Else
For Each lvi As ListViewItem In sel
ListView1.Items.Insert(hit.Item.Index, lvi.Clone)
Next
End If
End Sub
End Class
Listview_ordenar_items_con_arrastrar_y_soltar
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder