用C#实现在ListBox中拖动排序
https://blog.csdn.net/J_S_S/article/details/1635012
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Public Class Form1
Private indexofsource As Integer
'拖动的起始索引
Private indexoftarget As Integer
'拖动的结束索引
Private Sub listBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
indexofsource = CType(sender, ListBox).IndexFromPoint(e.X, e.Y)
If indexofsource <> ListBox.NoMatches Then
CType(sender, ListBox).DoDragDrop(CType(sender, ListBox).Items(indexofsource).ToString(), DragDropEffects.All)
End If
End Sub
Private Sub listBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragOver
'拖动源和放置的目的地一定是一个ListBox
If e.Data.GetDataPresent(GetType(System.String)) AndAlso CType(sender, ListBox).Equals(ListBox1) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub listBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
Dim listbox__1 As ListBox = CType(sender, ListBox)
indexoftarget = listbox__1.IndexFromPoint(listbox__1.PointToClient(New Point(e.X, e.Y)))
If indexoftarget <> ListBox.NoMatches Then
Dim temp As String = listbox__1.Items(indexoftarget).ToString()
listbox__1.Items(indexoftarget) = listbox__1.Items(indexofsource)
listbox__1.Items(indexofsource) = temp
listbox__1.SelectedIndex = indexoftarget
End If
End Sub
End Class
【用VB.net实现在ListBox中拖动排序.rar】点击下载此文件