Listview/Treeview "Click" events

Started by Paul Squires, October 31, 2009, 09:51:19 PM

Previous topic - Next topic

Paul Squires

A common question is how to handle "click" events for a Listview or a Treeview. Click events left/right, single and double-click events on a Listview/Treeview item.

The following is a good solution to dealing with this type of question. Pay attention to the fact that the message is dealt with in the CUSTOM message handler for the FORM. We could have have also dealt with it in the WM_NOTIFY handler if we wanted to.



Function FORM1_CUSTOM ( _
                      hWndForm      As Dword, _  ' handle of Form
                      wMsg          As Long,  _  ' type of message
                      wParam        As Dword, _  ' first message parameter
                      lParam        As Long   _  ' second message parameter
                      ) As Long

 
  Local pNMLV As NM_LISTVIEW Ptr
  ' Local pNMTV As NM_TREEVIEW Ptr   '<--- use this pointer for Treeviews
 
  Select Case wMsg
     
     Case %WM_NOTIFY
            pNMLV = lParam
           
            Select Case @pNMLV.hdr.idfrom
           
                 Case IDC_FORM1_LISTVIEW1        ' notify message from the listview control
                     
                     Select Case @pNMLV.hdr.code

                           Case %NM_CLICK    'left click         
                           Case %NM_DBLCLK   'left double-click
                           Case %NM_RCLICK   ' right click
                           Case %NM_RDBLCLK   'right double-click

                     End Select
                     
            End Select                     
 
  End Select
 
End Function

Paul Squires
PlanetSquires Software

Robert Rioja

Thank you Paul.  I will try it tomorrow.
Robert

Robert Rioja

Paul,
I tried it with a treeview (changed all listview references to treeview) but it does not work.  Can you try it?
Robert

Paul Squires

The code below works. Try it for yourself to see if it works for you. In your project, you probably need to remove other code where you are trying to catch the right click on a treeview node because it may  interfere with the code below.




Type TV_HANDLES
   nNode1 As Long
   nNode2 As Long
   nNode3 As Long
End Type

Global tvNodes As TV_HANDLES

'--------------------------------------------------------------------------------
Function FORM1_WM_CREATE ( _
                         hWndForm As Dword, _      ' handle of Form
                         ByVal UserData As Long _  ' optional user defined Long value
                         ) As Long

   tvNodes.nNode1 = FF_TreeView_InsertItem( HWND_FORM1_TREEVIEW1, %TVI_ROOT, "First Item" )
   tvNodes.nNode2 = FF_TreeView_InsertItem( HWND_FORM1_TREEVIEW1, %TVI_ROOT, "Second Item" )
   tvNodes.nNode3 = FF_TreeView_InsertItem( HWND_FORM1_TREEVIEW1, %TVI_ROOT, "Third Item" )

   Function = 0   ' change according to your needs
End Function


'--------------------------------------------------------------------------------
Function FORM1_CUSTOM ( _
                      hWndForm      As Dword, _  ' handle of Form
                      wMsg          As Long,  _  ' type of message
                      wParam        As Dword, _  ' first message parameter
                      lParam        As Long   _  ' second message parameter
                      ) As Long

  Local pNMTV As NM_TREEVIEW Ptr   '<--- use this pointer for Treeviews
 
  Select Case wMsg
     
     Case %WM_NOTIFY
            pNMTV = lParam
           
            Select Case @pNMTV.hdr.idfrom
           
                 Case IDC_FORM1_TREEVIEW1       
                     
                     Select Case @pNMTV.hdr.Code

                           Case %NM_CLICK    'left click         
                           Case %NM_DBLCLK   'left double-click
                           Case %NM_RCLICK   ' right click
                              Dim nNode As Long
                              nNode = FF_TreeView_GetSelection( HWND_FORM1_TREEVIEW1 )
                              MsgBox( "Node right clicked: " & Str$(nNode) )
                           Case %NM_RDBLCLK   'right double-click

                     End Select
                     
            End Select                     
 
  End Select
 
End Function


Paul Squires
PlanetSquires Software