+ -
当前位置:首页 → 问答吧 → VB如何调用Listview的ColumnClick事件

VB如何调用Listview的ColumnClick事件

时间:2011-12-25

来源:互联网

'以下排序代码测试没有问题的。现在想通过调用这个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
'=======================================================

作者: mmxmmx1122   发布时间: 2011-12-25

排序是这个事件里实现,但默认是按文本。
如果需要按数字、日期、IP地址等排序,需要用到API。

作者: vansoft   发布时间: 2011-12-26

热门下载

更多