2020年5月19日 星期二

vb.net 加入listview column click [Listview column click]


 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
#Region "List View Click Events"

    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs)

        ' Connect the ListView.ColumnClick event to the ColumnClick event handler.

        AddHandler Me.ListView1.ColumnClick, AddressOf ListView1_ColumnClick

        ' Set to Details view.

        Me.ListView1.View = View.Details


        ' Create an instance of the ColHeader class.

        Dim clickedCol As ColHeader = CType(Me.ListView1.Columns(e.Column), ColHeader)


        ' Set the ascending property to sort in the opposite order.

        clickedCol.ascending = Not clickedCol.ascending



        ' Get the number of items in the list.

        Dim numItems As Integer = Me.ListView1.Items.Count


        ' Turn off display while data is repoplulated.

        Me.ListView1.BeginUpdate()


        ' Populate an ArrayList with a SortWrapper of each list item.

        Dim SortArray As New ArrayList

        Dim i As Integer

        For i = 0 To numItems - 1

            SortArray.Add(New SortWrapper(Me.ListView1.Items(i), e.Column))

        Next i

        ' Sort the elements in the ArrayList using a new instance of the SortComparer

        ' class. The parameters are the starting index, the length of the range to sort,

        ' and the IComparer implementation to use for comparing elements. Note that

        ' the IComparer implementation (SortComparer) requires the sort 

        ' direction for its constructor; true if ascending, othwise false.

        SortArray.Sort(0, SortArray.Count, New SortWrapper.SortComparer(clickedCol.ascending))


        ' Clear the list, and repopulate with the sorted items.

        Me.ListView1.Items.Clear()

        Dim z As Integer

        For z = 0 To numItems - 1

            Me.ListView1.Items.Add(CType(SortArray(z), SortWrapper).sortItem)

        Next z

        ' Turn display back on.

        Me.ListView1.EndUpdate()

    End Sub


#End Region



    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        AddHandler Me.ListView1.ColumnClick, AddressOf ListView1_ColumnClick

    End Sub

沒有留言:

張貼留言