Public Sub ListView_ColumnClick(ByRef MyListView As ListView, ByVal ColumnHeader As ColumnHeader) With MyListView .Sorted = False If .SortKey <> ColumnHeader.Index - 1 Then .SortKey = ColumnHeader.Index - 1 .SortOrder = lvwAscending Else If .SortOrder = lvwAscending Then .SortOrder = lvwDescending Else .SortOrder = lvwAscending End If End If .Sorted = True End With End Sub
以下是用文本框及按钮来控制ListView的排序,供楼主参考.Private Sub Command2_Click() ListView1.SortKey = Val(Text1.Text) ListView1.SortOrder = lvwAscending ListView1.Sorted = True End Sub
假定用 Ctrl 键加数字键,数字表示排序的列号:Private Sub Fprm_Load() Me.KeyPreview = True End SubPrivate Sub Form_KeyDown(keycode As Integer, shift As Integer) If (Shift And vbShiftMask) Then With MyListView If keycode >= &H30 And keycode < &H30 + .ColumnHeaders.Count Then .Sorted = False If .SortKey <> keycode - &H30 Then .SortKey = keycode - &H30 .SortOrder = lvwAscending Else If .SortOrder = lvwAscending Then .SortOrder = lvwDescending Else .SortOrder = lvwAscending End If End If .Sorted = True End If End Sub
With MyListView
.Sorted = False
If .SortKey <> ColumnHeader.Index - 1 Then
.SortKey = ColumnHeader.Index - 1
.SortOrder = lvwAscending
Else
If .SortOrder = lvwAscending Then
.SortOrder = lvwDescending
Else
.SortOrder = lvwAscending
End If
End If
.Sorted = True
End With
End Sub
ListView1.SortKey = Val(Text1.Text)
ListView1.SortOrder = lvwAscending
ListView1.Sorted = True
End Sub
Me.KeyPreview = True
End SubPrivate Sub Form_KeyDown(keycode As Integer, shift As Integer)
If (Shift And vbShiftMask) Then
With MyListView
If keycode >= &H30 And keycode < &H30 + .ColumnHeaders.Count Then
.Sorted = False
If .SortKey <> keycode - &H30 Then
.SortKey = keycode - &H30
.SortOrder = lvwAscending
Else
If .SortOrder = lvwAscending Then
.SortOrder = lvwDescending
Else
.SortOrder = lvwAscending
End If
End If
.Sorted = True
End If
End Sub