PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Eddy Van Esch on May 20, 2015, 06:04:06 AM

Title: ListView: how to insert a row?
Post by: Eddy Van Esch on May 20, 2015, 06:04:06 AM
Hi all,

I have a ListView control.
A selected row can be deleted like this:

Function TBMIGS_CMDDELETEROW_BN_CLICKED ( _
                                        ControlIndex     As Long,  _  ' index in Control Array
                                        hWndForm         As Dword, _  ' handle of Form
                                        hWndControl      As Dword, _  ' handle of Control
                                        idButtonControl  As Long   _  ' identifier of button
                                        ) As Long
    Local i As Long
   
        'Delete the selected row in the ListView
    i = FF_ListView_GetSelectedItem( HWND_TBMIGS_LISTVIEW1 )
    If i = -1 Then Exit Function
   
    FF_ListView_DeleteItem( HWND_TBMIGS_LISTVIEW1, i )
   
End Function


However, I would also like to insert a row before (or after) the currently selected row.
I can't find a way to do this.
FF_ListView_InsertItem sounds promising, but it doesn't insert a row, it just inserts text in a specified cell.

I looked at Jose's Windows API Wrappers Reference manual to find:
FUNCTION ListView_InsertItem( BYVAL hwndLV AS DWORD, BYREF pitem AS LVITEM ) AS LONG
which might do the trick..?
Only .. the LVITEM structure seems rather cryptic to me.

TYPE LVITEMA
mask       AS DWORD
iItem      AS LONG
iSubItem   AS LONG
state      AS DWORD
stateMask  AS DWORD
pszText    AS ASCIIZ PTR
cchTextMax AS LONG
iImage     AS LONG
lParam     AS LONG
iIndent    AS LONG
iGroupId   AS LONG
cColumns   AS DWORD
puColumns  AS DWORD PTR
END TYPE

Has anybody used this? Or do you know a simple way to insert a row in a LV ?

Kind regards
Title: Re: ListView: how to insert a row?
Post by: Eddy Van Esch on May 20, 2015, 07:06:31 AM
Turned out to be a piece of cake!  :)
Just needed to figure out the LVITEM struct.


Function TBMIGS_CMDINSERTROW_BN_CLICKED ( _
                                        ControlIndex     As Long,  _  ' index in Control Array
                                        hWndForm         As Dword, _  ' handle of Form
                                        hWndControl      As Dword, _  ' handle of Control
                                        idButtonControl  As Long   _  ' identifier of button
                                        ) As Long
    Local i As Long
    'Local pItem As LVITEM
    Local lvi As LVITEM
    Local sTxt As String
   
        'Insert a row in the ListView
    i = FF_ListView_GetSelectedItem( HWND_TBMIGS_LISTVIEW1 )
    If i = -1 Then Exit Function
   
    lvi.iItem = i
    ListView_InsertItem (HWND_TBMIGS_LISTVIEW1, lvi )

End Function


Kind regards
Title: Re: ListView: how to insert a row?
Post by: Eddy Van Esch on May 20, 2015, 07:11:18 AM
Or this ....  :)
Looking at the code of FF_ListView_InsertItem helped ....  ;)


Function TBMIGS_CMDINSERTROW_BN_CLICKED ( _
                                        ControlIndex     As Long,  _  ' index in Control Array
                                        hWndForm         As Dword, _  ' handle of Form
                                        hWndControl      As Dword, _  ' handle of Control
                                        idButtonControl  As Long   _  ' identifier of button
                                        ) As Long
    Local i As Long
    Local sTxt As String
   
        'Insert a row in the ListView
    i = FF_ListView_GetSelectedItem( HWND_TBMIGS_LISTVIEW1 )
    If i = -1 Then Exit Function
   
    sTxt = ""
    FF_ListView_InsertItem( HWND_TBMIGS_LISTVIEW1, i, 0, sTxt, 0, 0 )
End Function