Const LVM_FINDITEM = LVM_FIRST + 13
Сообщение LVM_FINDITEM ищет элемент в списке по имени или по строке по заданным характеристикам.
В успешном случае возвращается индекс элемента, в случае ошибки возвращается -1
' Сортировка элементов списка ListView по дате
' ListView позволяет сортировать по алфавиту (свойство Sorted), но не по дате
' В меню Project щелкните на Components, выберите Microsoft Windows Common Controls 5.0 или Microsoft Windows Common Controls 6.0
' Добавьте в проект модуль Module1
' Добавьте на форму ListView
' Код для формы Form1
Option Explicit
Private Sub Form_Load()
Dim clmAdd As ColumnHeader
Dim itmAdd As ListItem
'Add two Column Headers to the ListView control
Set clmAdd = ListView1.ColumnHeaders.Add(Text:="Имя")
Set clmAdd = ListView1.ColumnHeaders.Add(Text:="Дата рождения")
'Set the view property of the Listview control to Report view
ListView1.View = lvwReport
'Add data to the ListView control
Set itmAdd = ListView1.ListItems.Add(Text:="Васька")
itmAdd.SubItems(1) = "05/07/97"
Set itmAdd = ListView1.ListItems.Add(Text:="Барсик")
itmAdd.SubItems(1) = "04/08/97"
Set itmAdd = ListView1.ListItems.Add(Text:="Рыжик")
itmAdd.SubItems(1) = "05/29/97"
Set itmAdd = ListView1.ListItems.Add(Text:="Мурзик")
itmAdd.SubItems(1) = "05/17/97"
Set itmAdd = ListView1.ListItems.Add(Text:="Гарфилд")
itmAdd.SubItems(1) = "04/01/97"
End Sub
' Use this line of code for Common Controls 5.0:
' Private Sub ListView1_ColumnClick(ByVal ColumnHeader As _
ComctlLib.ColumnHeader)
' For Common Controls 6.0, uncomment the line below and comment the line above.
Private Sub ListView1_ColumnClick(ByVal ColumnHeader As _
MSComctlLib.ColumnHeader)
Dim strName As String
Dim dDate As Date
Dim lngItem As Long
'Handle User click on column header
If ColumnHeader.Text = "Name" Then 'User clicked on Name header
ListView1.Sorted = True 'Use default sorting to sort the
ListView1.SortKey = 0 'items in the list
Else
ListView1.Sorted = False 'User clicked on the Date header
'Use our sort routine to sort
'by date
SendMessage ListView1.hWnd, _
LVM_SORTITEMS, _
ListView1.hWnd, _
AddressOf CompareDates
End If
'Refresh the ListView before writing the data
ListView1.Refresh
'Loop through the items in the List to print them out in
'sorted order.
'NOTE: You are looping through the ListView control because when _
'sorting by date the ListItems collection won't be sorted.
For lngItem = 0 To ListView1.ListItems.Count - 1
ListView_GetListItem lngItem, ListView1.hWnd, strName, dDate
Next
End Sub
' Код для модуля
Option Explicit
Public Type POINT
x As Long
y As Long
End Type
Public Type LV_FINDINFO
flags As Long
psz As String
lParam As Long
pt As POINT
vkDirection As Long
End Type
Public Type LV_ITEM
mask As Long
iItem As Long
iSubItem As Long
State As Long
stateMask As Long
pszText As Long
cchTextMax As Long
iImage As Long
lParam As Long
iIndent As Long
End Type
Private Const LVFI_PARAM = 1
Private Const LVIF_TEXT = &H1
Private Const LVM_FIRST = &H1000
Private Const LVM_FINDITEM = LVM_FIRST + 13
Private Const LVM_GETITEMTEXT = LVM_FIRST + 45
Public Const LVM_SORTITEMS = LVM_FIRST + 48
Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
'Module Functions and Procedures
'CompareDates: This is the sorting routine that gets passed to the
'ListView control to provide the comparison test for date values.
Public Function CompareDates(ByVal lngParam1 As Long, _
ByVal lngParam2 As Long, _
ByVal hWnd As Long) As Long
Dim strName1 As String
Dim strName2 As String
Dim dDate1 As Date
Dim dDate2 As Date
'Obtain the item names and dates corresponding to the
'input parameters
ListView_GetItemData lngParam1, hWnd, strName1, dDate1
ListView_GetItemData lngParam2, hWnd, strName2, dDate2
'Compare the dates
'Return 0 ==> Less Than
' 1 ==> Equal
' 2 ==> Greater Than
If dDate1 < dDate2 Then
CompareDates = 0
ElseIf dDate1 = dDate2 Then
CompareDates = 1
Else
CompareDates = 2
End If
End Function
'GetItemData - Given Retrieves
Public Sub ListView_GetItemData(lngParam As Long, _
hWnd As Long, _
strName As String, _
dDate As Date)
Dim objFind As LV_FINDINFO
Dim lngIndex As Long
Dim objItem As LV_ITEM
Dim baBuffer(32) As Byte
Dim lngLength As Long
'
' Convert the input parameter to an index in the list view
'
objFind.flags = LVFI_PARAM
objFind.lParam = lngParam
lngIndex = SendMessage(hWnd, LVM_FINDITEM, -1, VarPtr(objFind))
'
' Obtain the name of the specified list view item
'
objItem.mask = LVIF_TEXT
objItem.iSubItem = 0
objItem.pszText = VarPtr(baBuffer(0))
objItem.cchTextMax = UBound(baBuffer)
lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
VarPtr(objItem))
strName = Left$(StrConv(baBuffer, vbUnicode), lngLength)
'
' Obtain the modification date of the specified list view item
'
objItem.mask = LVIF_TEXT
objItem.iSubItem = 1
objItem.pszText = VarPtr(baBuffer(0))
objItem.cchTextMax = UBound(baBuffer)
lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
VarPtr(objItem))
If lngLength > 0 Then
dDate = CDate(Left$(StrConv(baBuffer, vbUnicode), lngLength))
End If
End Sub
'GetListItem - This is a modified version of ListView_GetItemData
' It takes an index into the list as a parameter and returns
' the appropriate values in the strName and dDate parameters.
Public Sub ListView_GetListItem(lngIndex As Long, _
hWnd As Long, _
strName As String, _
dDate As Date)
Dim objItem As LV_ITEM
Dim baBuffer(32) As Byte
Dim lngLength As Long
'
' Obtain the name of the specified list view item
'
objItem.mask = LVIF_TEXT
objItem.iSubItem = 0
objItem.pszText = VarPtr(baBuffer(0))
objItem.cchTextMax = UBound(baBuffer)
lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
VarPtr(objItem))
strName = Left$(StrConv(baBuffer, vbUnicode), lngLength)
'
' Obtain the modification date of the specified list view item
'
objItem.mask = LVIF_TEXT
objItem.iSubItem = 1
objItem.pszText = VarPtr(baBuffer(0))
objItem.cchTextMax = UBound(baBuffer)
lngLength = SendMessage(hWnd, LVM_GETITEMTEXT, lngIndex, _
VarPtr(objItem))
If lngLength > 0 Then
dDate = CDate(Left$(StrConv(baBuffer, vbUnicode), lngLength))
End If
End Sub