File size: 4,522 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms

Namespace Controls

    Public Class ListViewEx

        Inherits ListView

        Private Const REORDER As String = "Reorder"

        Private _allowRowReorder As Boolean = True

        Public Property AllowRowReorder As Boolean
            Get
                Return Me._allowRowReorder
            End Get
            Set
                Me._allowRowReorder = Value
                MyBase.AllowDrop = Value
            End Set
        End Property

        Public Shadows Property Sorting As SortOrder
            Get
                Return SortOrder.None
            End Get
            Set
                MyBase.Sorting = SortOrder.None
            End Set
        End Property

        Public Sub New()
            MyBase.New
            Me.AllowRowReorder = True
        End Sub

        Protected Overrides Sub OnDragDrop(ByVal e As DragEventArgs)
            MyBase.OnDragDrop(e)
            If Not Me.AllowRowReorder Then
                Return
            End If

            If (MyBase.SelectedItems.Count = 0) Then
                Return
            End If

            Dim cp As Point = MyBase.PointToClient(New Point(e.X, e.Y))
            Dim dragToItem As ListViewItem = MyBase.GetItemAt(cp.X, cp.Y)
            If (dragToItem Is Nothing) Then
                Return
            End If

            Dim dropIndex As Integer = dragToItem.Index
            If (dropIndex > MyBase.SelectedItems(0).Index) Then
                dropIndex = (dropIndex + 1)
            End If

            Dim insertItems As ArrayList = New ArrayList(MyBase.SelectedItems.Count)
            For Each item As ListViewItem In MyBase.SelectedItems
                insertItems.Add(item.Clone)
            Next
            Dim i As Integer = (insertItems.Count - 1)
            Do While (i >= 0)
                Dim insertItem As ListViewItem = CType(insertItems(i), ListViewItem)
                MyBase.Items.Insert(dropIndex, insertItem)
                i = (i - 1)
            Loop

            For Each removeItem As ListViewItem In MyBase.SelectedItems
                MyBase.Items.Remove(removeItem)
            Next
        End Sub

        Protected Overrides Sub OnDragOver(ByVal e As DragEventArgs)
            If Not Me.AllowRowReorder Then
                e.Effect = DragDropEffects.None
                Return
            End If

            If Not e.Data.GetDataPresent(DataFormats.Text) Then
                e.Effect = DragDropEffects.None
                Return
            End If

            Dim cp As Point = MyBase.PointToClient(New Point(e.X, e.Y))
            Dim hoverItem As ListViewItem = MyBase.GetItemAt(cp.X, cp.Y)
            If (hoverItem Is Nothing) Then
                e.Effect = DragDropEffects.None
                Return
            End If

            For Each moveItem As ListViewItem In MyBase.SelectedItems
                If (moveItem.Index = hoverItem.Index) Then
                    e.Effect = DragDropEffects.None
                    hoverItem.EnsureVisible()
                    Return
                End If

            Next
            MyBase.OnDragOver(e)
            Dim text As String = CType(e.Data.GetData(REORDER.GetType), String)
            If (text.CompareTo(REORDER) = 0) Then
                e.Effect = DragDropEffects.Move
                hoverItem.EnsureVisible()
            Else
                e.Effect = DragDropEffects.None
            End If

        End Sub

        Protected Overrides Sub OnDragEnter(ByVal e As DragEventArgs)
            MyBase.OnDragEnter(e)
            If Not Me.AllowRowReorder Then
                e.Effect = DragDropEffects.None
                Return
            End If

            If Not e.Data.GetDataPresent(DataFormats.Text) Then
                e.Effect = DragDropEffects.None
                Return
            End If

            MyBase.OnDragEnter(e)
            Dim text As String = CType(e.Data.GetData(REORDER.GetType), String)
            If (text.CompareTo(REORDER) = 0) Then
                e.Effect = DragDropEffects.Move
            Else
                e.Effect = DragDropEffects.None
            End If

        End Sub

        Protected Overrides Sub OnItemDrag(ByVal e As ItemDragEventArgs)
            MyBase.OnItemDrag(e)
            If Not Me.AllowRowReorder Then
                Return
            End If

            MyBase.DoDragDrop(REORDER, DragDropEffects.Move)
        End Sub
    End Class

End Namespace