VB and VBA Users Source Code: Determining which item was clicked in a listview
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Determining which item was clicked in a listview
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, May 30, 2001
Hits:
935
Category:
Windows API
Article:
The following code demonstrates how to determine which item on a listview was clicked and where exactly on the item the mouse was clicked. 'PLACE IN A STANDARD MODULE Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal lMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Type POINTAPI x As Long y As Long End Type Public Type LVHITTESTINFO pt As POINTAPI lFlags As Long lItem As Long lSubItem As Long End Type Public Const LVHT_NOWHERE = &H1, LVHT_ONITEMICON = &H2 Public Const LVHT_ONITEMLABEL = &H4, LVHT_ONITEMSTATEICON = &H8 Public Const LVHT_ONITEM = (LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_ONITEMSTATEICON) 'Purpose : Determines which item (if any) was clicked on a listview 'Inputs : lvhwndTest The handle to the listview ' x The x coordinate of the mouse (in twips) ' y The y coordinate of the mouse (in twips) ' tItemClicked See Outputs 'Outputs : Returns True on success. ' Populates the tItemClicked structure with information regarding the item clicked 'Author : Andrew Baker 'Date : 25/04/2001 'Notes : Call this routine from the MouseDown event of a listview, eg: ' 'Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) ' LVItemClicked ListView1.hwnd, x, y, tItemClicked Function LVItemClicked(lvhwndTest As Long, x As Single, y As Single, tItemClicked As LVHITTESTINFO) As Boolean Const LVM_FIRST = &H1000&, LVM_SUBITEMHITTEST = (LVM_FIRST + 57) Dim lRet As Long Dim lX As Long, lY As Long On Error Resume Next 'Convert x and y from twips to pixels lX = x / Screen.TwipsPerPixelX lY = y / Screen.TwipsPerPixelY With tItemClicked .lFlags = 0 .lItem = 0 .lSubItem = 0 .pt.x = lX .pt.y = lY End With 'Populate structure with details of the item clicked lRet = SendMessage(lvhwndTest, LVM_SUBITEMHITTEST, 0, tItemClicked) If Err.LastDllError = 0 Then LVItemClicked = True End If On Error GoTo 0 End Function 'PLACE IN A FORM 'Demonstration routine, with a listview called ListView1 on it Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Dim tItemClicked As LVHITTESTINFO Dim sItemClicked As String 'Call the function to determine which item was clicked LVItemClicked ListView1.hwnd, x, y, tItemClicked sItemClicked = "Item " & tItemClicked.lItem + 1 & ", SubItem " & tItemClicked.lSubItem Select Case tItemClicked.lFlags Case LVHT_NOWHERE 'clicked nothing sItemClicked = "Nowhere" Case LVHT_ONITEMLABEL 'clicked text sItemClicked = sItemClicked & ", On Label" Case LVHT_ONITEMSTATEICON 'clicked on a state icon sItemClicked = sItemClicked & ", On State Icon" Case LVHT_ONITEMICON 'clicked icon sItemClicked = sItemClicked & ", On Icon" End Select MsgBox sItemClicked End Sub 'Populate the listview Private Sub Form_Load() Dim oListitem As ListItem Dim lThisRow As Long For lThisRow = 1 To 255 Set oListitem = ListView1.ListItems.Add(, , "Row " & lThisRow) oListitem.ListSubItems.Add , , "Column 2" oListitem.ListSubItems.Add , , "Column 3" Next End Sub
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder