Announcement

Collapse
No announcement yet.

Select All Rows In ListView

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select All Rows In ListView

    How do I select all rows in a ListView?

    I have this function to de-select them.

    Code:
    FUNCTION ListView_UnselectAll (byval Winhandle as dword, BYVAL Dlg as long) AS LONG
    
    local TempDword1 as dword
    
    CONTROL HANDLE Winhandle, Dlg TO TempDword1
    ListView_SetItemState TempDword1, -1, BYVAL %NULL, %LVIS_SELECTED OR %LVIS_FOCUSED
    
    END FUNCTION
    Last edited by Steve Bouffe; 11 Jul 2009, 06:27 AM.

  • #2
    Hint: Parameter three of Listview_SetItemState
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Try the following:

      Code:
         Local LV As LV_ITEM
         
         LV.mask      = %LVIF_STATE
         LV.State     = -1 
         LV.stateMask = %LVIS_SELECTED
            
         SendMessage hwndListView, %LVM_SETITEMSTATE, -1, VarPtr(LV)
      Paul Squires
      FireFly Visual Designer (for PowerBASIC Windows 10+)
      Version 3 now available.
      http://www.planetsquires.com

      Comment


      • #4
        functions for multi-select listboxes

        Added: Coming back and seeing this a day later, I realized I responded with code for ListBox, not ListView. I'll leave the code, and I apologize for the misdirection.
        -jhm


        Here are some of my routines, which include both a SelectAll and an UnSelectAll.

        Just last week I started writing an app in FireFly that uses a multiple-selection listbox. FF doesn't support certain Select function on them, so I had to go to the API. Also, CommCtrl.inc does not contain some of the functions for multi-select ListBox that are documented in the SDK, but I've tried them and seem to be successful.

        These routines work fine in my app, but I don't think I finished addressing universal coding issues... Try them, though. Maybe you can tell me if I missed anything.


        Note that my XferSelections2Ary routine is shared by the one function that copies selected lines to the clipboard and the one that writes selected lines to file. It may not be efficient or necessary - it was in my original design, and I haven't cleaned everything up yet.

        I hope these help!
        -John (the phantom)

        note that Listboxes are 0-based, thus several i-1 adustments...

        Code:
        Function FRMMAIN_CMDSELECTALL_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, ItemCount, lReturn, CaretNdx As Long
        
           ItemCount = Listbox_GetCount( HWND_FRMMAIN_LSTDATES)
           lReturn = Listbox_SelItemRange (HWND_FRMMAIN_LSTDATES, %TRUE, 0, ItemCount -1) 
        End Function
        
        Function Listbox_SelItemRange (ByVal hListBox As Dword, ByVal fSelect As Long, ByVal FirstItem As Long, ByVal LastItem As Long) As Long
        'Select/deselect one or more consecutive items in a multiple-selection list box. 
        'If use this func to select 1 item, you'd need to select a range of the item and one adjacent, then deselect the adjacent...
           'Set fSelect to True to select & highlight the string; to False to remove highlight and deselect the string.
           'For ItemRange:
           '  The low-order  word (16 bits) specifies the zero-based index of the first item to select. 
           '  The high-order word (16 bits) specifies the zero-based index of the last item to select. 
           Local ItemRange As Long
           ItemRange = (LastItem * 256) + FirstItem    'works, but not sure it's correct...
           Function = SendMessage(hListBox, %LB_SELITEMRANGE, fSelect, ItemRange) 
        End Function                                               
        '--------------------------------------------------------------------------------
        '--------------------------------------------------------------------------------
        Function FRMMAIN_CMDDELETE1STSEL_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, ItemCount, lReturn As Long
        
           ItemCount = Listbox_GetCount( HWND_FRMMAIN_LSTDATES)
           For i = 1 To ItemCount
              If Listbox_GetSel(HWND_FRMMAIN_LSTDATES, i-1) Then
                 Listbox_DeleteString(HWND_FRMMAIN_LSTDATES, i-1)
                 Exit For
              End If 
           Next i 
           Function = i   'the item deleted   
        End Function
        '--------------------------------------------------------------------------------
        Function FRMMAIN_CMDINVERTSELS_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, ItemCount, lReturn As Long
        
           ItemCount = Listbox_GetCount( HWND_FRMMAIN_LSTDATES)
           For i = 1 To ItemCount
              'Invert the selected with the unselected...
              If Listbox_GetSel(HWND_FRMMAIN_LSTDATES, i-1) Then
                 lReturn = Listbox_SetSel(HWND_FRMMAIN_LSTDATES, %FALSE, i-1)
              Else 
                 lReturn = Listbox_SetSel(HWND_FRMMAIN_LSTDATES, %TRUE, i-1)
              End If 
           Next i 
        End Function
        '--------------------------------------------------------------------------------
        Function FRMMAIN_CMDUNSELECTALL_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, ItemCount, lReturn As Long
        
           ItemCount = Listbox_GetCount( HWND_FRMMAIN_LSTDATES)
           lReturn = Listbox_SelItemRange (HWND_FRMMAIN_LSTDATES, %FALSE, 0, ItemCount -1) 
        End Function                                                                                         
        '--------------------------------------------------------------------------------
        '--------------------------------------------------------------------------------
        Function FRMMAIN_CMDCOPY2CLIP_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 lCount As Long, FoundDates(), TheText As String
           
           lCount = XferSelections2Ary (HWND_FRMMAIN_LSTDATES, FoundDates())  'prep by xferring from LB to an ary() 
           ReDim FoundDates(1 To lCount)
           If lCount < 0 Then 
               MsgBox "Error in transferring data from listbox to internal array.",,"Copy to clipboard error"
               Exit Function
           End If 
           TheText = Join$(FoundDates(), $CrLf) 
           FF_ClipboardSetText(TheText)        
           mRising
        End Function
        '--------------------------------------------------------------------------------
        Function XferSelections2Ary (ByVal LstBox As Long, Ary() As String) As Long
           'copy all the SELECTED dates from the ListBox into the array provided, and trim empty rows from array...
           Local ItemsSel, ItemCount, i As Long                                                                                                
           Local ndx As Long
        
           ItemsSel = Listbox_GetSelCount(HWND_FRMMAIN_LSTDATES)   '
           If ItemsSel = 0 Then 
              MsgBox "There are no selected dates!",,"No Selection" 
              Exit Function
           End If
        
           ItemCount = FF_ListBox_GetCount(HWND_FRMMAIN_LSTDATES)   '
           ReDim Ary(1 To ItemCount)
           ndx = 0
           
           ItemCount = Listbox_GetCount( HWND_FRMMAIN_LSTDATES)
           For i = 1 To ItemCount
              If Listbox_GetSel(HWND_FRMMAIN_LSTDATES, i-1) Then
                 Incr ndx
                 Ary(ndx) = FF_ListBox_GetText (HWND_FRMMAIN_LSTDATES, i-1)     
              End If 
           Next i 
        
           ReDim Preserve Ary(1 To ndx)  'get rid of excess                                    
           'return the 1-based count of items transferred
           Function = ndx
        End Function
        '--------------------------------------------------------------------------------
        Function FRMMAIN_CMDSAVE2FILE_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 nResult   As Long      ' determines whether the OpenFiles dialog was cancelled 
           Local sFilename As String    ' holds the returned list of selected file(s) 
           Local sFilter   As String    ' holds the filter list    
           Local nFlags    As Long      ' flags that descibe the OpenFiles dialog behavior 
           Local ItemsSel  As Long
                   
           ItemsSel = Listbox_GetSelCount(HWND_FRMMAIN_LSTDATES)   '
           If ItemsSel = 0 Then 
              MsgBox "There are no selected dates!",,"No Selection" 
              Exit Function
           End If
        
           sFilter = "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc|All files (*.*)|*.*"      
           nFlags  = %OFN_HIDEREADONLY Or %OFN_LONGNAMES Or %OFN_OVERWRITEPROMPT Or %OFN_EXPLORER      
           nResult = FF_SaveFileDialog( hWndForm, "Save File As", sFilename, "", sFilter, "txt", nFlags, %True)      
           'see if the dialog was cancelled 
           If nResult = 0 Then Exit Function 
        
           'mRising   'audio notification macro
           
           Local lCount, i As Long, FoundDates(), TheText As String   
           ReDim FoundDates(1 To ItemsSel)
           nResult = XferSelections2Ary (HWND_FRMMAIN_LSTDATES, FoundDates())  'prep by xferring from LB to an ary() 
           If nResult < 0 Then 
               MsgBox "Error in transferring data from listbox to internal array.",,"Copy to clipboard error"
               Exit Function
           End If          
              
           TheText = Join$(FoundDates(), $CrLf) 
        
           Call WriteToFile(sFilename, TheText)
        
           'mFalling   'audio notification macro
        End Function
        Last edited by John Montenigro; 12 Jul 2009, 02:44 PM. Reason: clarify a msitake I made ;)

        Comment

        Working...
        X