读书人

VB怎么调用Listview的ColumnClick事件

发布时间: 2013-06-25 23:45:41 作者: rapoo

VB如何调用Listview的ColumnClick事件
'以下排序代码测试没有问题的。现在想通过调用这个Listview的ColumnClick事件来对Listveiw控件进行排序。请大家帮忙。

Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hWndLock As Long) As Long

Private Declare Function GetTickCount Lib "kernel32" () As Long

Sub ListView1_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

Dim lngStart As Long
lngStart = GetTickCount

With ListView1

Dim lngCursor As Long
lngCursor = .MousePointer
.MousePointer = vbHourglass

LockWindowUpdate .hWnd

Dim l As Long
Dim strFormat As String
Dim strData() As String

Dim lngIndex As Long
lngIndex = ColumnHeader.Index - 1

Select Case UCase$(ColumnHeader.Tag)

'排序日期==========================================
Case "DATE"

strFormat = "YYYYMMDDHhNnSs"

With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l


Else
For l = 1 To .Count
With .Item(l)
.Tag = .Text & Chr$(0) & .Tag
If IsDate(.Text) Then
.Text = Format(CDate(.Text), strFormat)
Else
.Text = ""
End If
End With
Next l
End If
End With


' .SortOrder = (.SortOrder + 1) Mod 2 '是否升序或者降序
.SortKey = ColumnHeader.Index - 1
.Sorted = True

With .ListItems
If (lngIndex > 0) Then
For l = 1 To .Count
With .Item(l).ListSubItems(lngIndex)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l


Else
For l = 1 To .Count
With .Item(l)
strData = Split(.Tag, Chr$(0))
.Text = strData(0)
.Tag = strData(1)
End With
Next l
End If
End With

End Select

LockWindowUpdate 0&

.MousePointer = lngCursor

End With
'=======================================================


[解决办法]
排序是这个事件里实现,但默认是按文本。
如果需要按数字、日期、IP地址等排序,需要用到API。
[解决办法]
call ListView1_ColumnClick(ListView1.ColumnHeaders(1))
其中的1就是对应第几列

读书人网 >VB

热点推荐