using ListView

Started by Shawn Anderson, December 17, 2010, 05:47:17 PM

Previous topic - Next topic

Shawn Anderson

I've created a listview with 4 columns and populated it with data.

When I click a row, I want to know which row was clicked so I can grab the data from those rows using FF_ListView_GetItemText.

I'm using FF_ListView_GetSelectedItem but it doesn't work right:
ndx=FF_ListView_GetSelectedItem( HWND_name)
I have to click 2 or 3 times to get the right row.

Any suggestions?
thanks
thanks

Elias Montoya


Try...

'--------------------------------------------------------------------------------
FUNCTION YOURFORM_LISTVIEW1_LVN_ITEMCHANGED ( _
                                               ControlIndex  As Long,            _  ' index in Control Array
                                               hWndForm      As Dword,           _  ' handle of Form
                                               hWndControl   As Dword,           _  ' handle of Control
                                               ByVal lpNMV   As NM_LISTVIEW Ptr  _  ' pointer to NM_LISTVIEW
                                               ) As Long

UpdatedRow = @lpNMV.iItem

END FUNCTION
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

Shawn Anderson

#2
thanks Elias:
that works, but only if I click the first column.

How can I get it to work when I click anywhere on the row?

Elias Montoya

#3
I would store @lpNMV.iItem in a global variable and catch wm_lbuttondown and just retrieve the index from the global variable.

That is because that notification is fired up only when the selection changes, if selection remains the same, just get the old position.
Win7, iMac x64 Retina display 5K, i7-5820K 4.4 ghz, 32GB RAM, All updates applied. - Firefly 3.70.

Rolf Brandt

#4
Hello Shawn,

you need to caüture that in the forms custom handler. Enclosed is a small example project with two listviews. The left listview would hold a recordset, the right one displays the data of the current record.

You catch the %NM_CLICK event. That will give you the correct row number. With this you are catching the MouseUp event.

Try the following:
Click on the Listview. Hold the mousebutton down. There is no change in the right listview yet. Release the mousebutton and you will get the correct record.

I can't tell you the Windows interna why LVN_ITEMCHANGED doesn't do it with the mouseclicks. Paul or Jose have an explanation for it. Would be nice also if the WM_LBUTTONUP would give you the proper value.
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Klaas Holland

Perhaps this can help.

Function FORMT1_CUSTOM (hWndForm As Dword, wMsg As Long, wParam As Dword, lParam As Long) As Long
    Local pNmLvw        As NM_LISTVIEW Ptr
    Local RectSub       As Rect
    Global gLvRow       As Long
    Global gLvCol       As Long

    Select Case wMsg

        Case %WM_NOTIFY
            Select Case LoWrd(wParam)
                Case IDC_FORMT1_LISTVIEW1
                    pNmLvw = lParam

                    If @pNmLvw.hdr.Code = %NM_CLICK And @pNmLvw.iItem > -1 Then 'Click in listview
                        gLvRow  = @pNmLvw.iItem
                        gLvCol  = @pNmLvw.iSubItem
                        EditCell

                    ElseIf @pNmLvw.hdr.Code = %NM_DBLCLK And @pNmLvw.iItem > -1 Then 'DoubleClick in listview
                        gLvRow  = @pNmLvw.iItem
                        gLvCol  = @pNmLvw.iSubItem
                        ComboCell

                    End If
                End Select

    End Select

End Function


Paul Squires

Paul Squires
PlanetSquires Software

Rolf Brandt

#7
I tried that out too, but it shows the correct data at the second click only. First click will always show the row number of the previous position.

So far for me only the code in the custom event handler works. (Basically the same as Klaas' code.)


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 lpNmh  As NMHDR Ptr
   Local lpLvNm As NM_LISTVIEW Ptr
   
   Select Case wMsg
      Case %WM_NOTIFY
         lpNmh = lParam
         
         Select Case @lpNmh.idFrom
            Case IDC_FORM1_LISTVIEW1
            lpLvNm = lParam
            Select Case @LpLvNm.hdr.Code
               Case  %NM_CLICK
                  Call DisplayRec
               Case Else
            End Select
         End Select     
      Case Else
   End Select
End Function

Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Shawn Anderson

Well I seem to be missing something here.
I can still only see which row was clicked when the first column is clicked.


'--------------------------------------------------------------------------------
Function bu_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 lpNmh  As NMHDR Ptr
   Local lpLvNm As NM_LISTVIEW Ptr
   Local lLvItem        As Long
   Local lLvSubItem    As Long
   Local ndx As Long

   Select Case wMsg
      Case %WM_NOTIFY
         lpNmh = lParam
         
         Select Case @lpNmh.idFrom
         
            Case IDC_bu_routerlist
            lpLvNm = lParam
            Select Case @LpLvNm.hdr.Code

               Case  %NM_CLICK
                  ' clicked
                  ndx=@lpLvNm.iItem
                 
                  MsgBox Str$(ndx)
               
               Case Else
            End Select
                     
         End Select
     
      Case Else
   End Select
End Function

Shawn Anderson

I had to turn on FullRowSelect to TRUE  :-[
works now, thanks everybody


Rolf Brandt

I was just wondering - thought it had to do something with the listview settings.
Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)