Mulitple item select in ListView

Share your advanced PureBasic knowledge/code with the community.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Mulitple item select in ListView

Post by josku_x »

Hello!

Here is simple API code for getting all the selected items in a ListViewGadget. Note that you have to use the #LBS_EXTENDEDSEL flag to select multiple items, do not use #LBS_MULTIPLESEL flag.

No credit needed. Have fun.

Code: Select all

Procedure.l GetSelectedItems(Gadget)
SelItems=SendMessage_(GadgetID(Gadget), #LB_GETSELCOUNT, 0, 0) ; Count the selected items.
If SelItems
  Dim ItemIndexes.l(SelItems) ; Create an array that will contain the items.
  ; Get the selected items and pass them into the array.
  SendMessage_(GadgetID(Gadget), #LB_GETSELITEMS, SelItems, @ItemIndexes())
  ; Get the last selected item (where the mouse was released).
  LastItem=SendMessage_(GadgetID(Gadget), #LB_GETCURSEL, 0, 0)
  ; Sort the array (this prevents some unwanted results).
  SortArray(ItemIndexes(), 0)
  ; Debug the selected items.
  Debug "Selected items:"
  For a=1 To SelItems
    Debug "> Item "+Str(ItemIndexes(a)+1)
  Next
  Debug "Mouse released on item "+Str(LastItem+1)
  Debug "--------"
EndIf
EndProcedure

; Create the window and process the events.
If OpenWindow(0,0,0,200,71,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Multiple items select")
  CreateGadgetList(WindowID())
  ; Note that you have to use #LBS_EXTENDEDSEL. Don't use #LBS_MULTIPLESEL!
  ListViewGadget(0,1,1,198,69, #LBS_EXTENDEDSEL)
  ; Add some items.
  For a=1 To 5
   AddGadgetItem(0, -1, "Item "+Str(a))
  Next
  ; Main program loop.
  Close=0
  Repeat
   Select WaitWindowEvent()
    Case #PB_Event_Gadget
     ; Get the user selected items in the ListViewGadget.
     If EventGadgetID()=0
      GetSelectedItems(0)
     EndIf
    ; Deselect all items whenever a right-click occurs on the ListViewGadget.
    Case #WM_RBUTTONDOWN
     If ChildWindowFromPoint_(WindowID(), WindowMouseX(), WindowMouseY())=GadgetID(0)
       SendMessage_(GadgetID(0), #LB_SETSEL, #False, -1)
     EndIf
    Case #PB_Event_CloseWindow
     Close=1
   EndSelect
  Until Close=1
EndIf
End
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

another example

Code: Select all

; purebasic survival guide - pb3.93
; multiselect_1.pb - 08.04.2005 ejn (blueznl)
; http://www.xs4all.nl/~bluez/datatalk/pure1.htm
;
; - a listviewgadget and a listicongadget, both with multiselect enabled
; - how to move the selecton through both gadgets
;

OpenWindow(1,400,200,300,300,#PB_Window_SystemMenu,"Test")
CreateGadgetList(WindowID())
h1 = ListViewGadget(1,10,10,80,270,#LBS_EXTENDEDSEL)
h2 = ListIconGadget(2,210,10,80,270,"test",30,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection|#PB_ListIcon_FullRowSelect)
AddGadgetColumn(2,1,"test",30)
ButtonGadget(3,110,10,80,110,"Up!")
ButtonGadget(4,110,170,80,110,"Down!")
TextGadget(5,110,140,80,20,Str(p),#PB_Text_Center)
;
For n = 65 To 65+25
  AddGadgetItem(1,-1,Chr(n))
  AddGadgetItem(2,-1,Str(n)+#LF$+Str(100-n))
Next n
;
done = #False
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      done = #True
    Case #PB_Event_Gadget
      Select EventGadgetID()
        Case 3
          ;
          SetGadgetItemState(1,p,0)
          SetGadgetItemState(2,p,0)
          p = p-1
          SetGadgetItemState(1,p,1)
          SetGadgetItemState(2,p,1)
          ;
        Case 4
          ;
          SetGadgetItemState(1,p,0)
          SetGadgetItemState(2,p,0)
          p = p+1
          SetGadgetItemState(1,p,1)
          SetGadgetItemState(2,p,1)
          ;
        Default
          Debug EventGadgetID()
      EndSelect
      SetGadgetText(5,Str(p))
  EndSelect
Until done
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

I don't want to be mean, but blueznl, your code doesn't seem to get the selected items. I don't understand what it should do as I am experiencing bugs in it, if they are bugs what I think.

Anyway, I want to hear some comments on my code if you people experience bugs, etc..
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Wow!! Exacly what I needed. You guys rock the ground 8)
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

No problem mate. API is the evil :twisted:
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: Mulitple item select in ListView

Post by Tranquil »

Hi Forum.

This one is nearly exactly what I need for my project but it only seems to work with a ListViewGadget maybe someone got this working on a ListIcongadget()?

What I want is a way to easily select/deselect Items while holding down the right mouse button.

Thanks in advance!

Code: Select all

Procedure.l GetSelectedItems(Gadget)
SelItems=SendMessage_(GadgetID(Gadget), #LB_GETSELCOUNT, 0, 0) ; Count the selected items.
If SelItems
  Dim ItemIndexes.l(SelItems) ; Create an array that will contain the items.
  ; Get the selected items and pass them into the array.
  SendMessage_(GadgetID(Gadget), #LB_GETSELITEMS, SelItems, @ItemIndexes())
  ; Get the last selected item (where the mouse was released).
  LastItem=SendMessage_(GadgetID(Gadget), #LB_GETCURSEL, 0, 0)
  ; Sort the array (this prevents some unwanted results).
  SortArray(ItemIndexes(), 0)
  ; Debug the selected items.
  Debug "Selected items:"
  For a=1 To SelItems
    Debug "> Item "+Str(ItemIndexes(a)+1)
  Next
  Debug "Mouse released on item "+Str(LastItem+1)
  Debug "--------"
EndIf
EndProcedure

; Create the window and process the events.
If OpenWindow(0,0,0,200,71,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Multiple items select")
  CreateGadgetList(WindowID())
  ; Note that you have to use #LBS_EXTENDEDSEL. Don't use #LBS_MULTIPLESEL!
  ListViewGadget(0,1,1,198,69, #LBS_EXTENDEDSEL)
  ; Add some items.
  For a=1 To 5
   AddGadgetItem(0, -1, "Item "+Str(a))
  Next
  ; Main program loop.
  Close=0
  Repeat
   Select WaitWindowEvent()
    Case #PB_Event_Gadget
     ; Get the user selected items in the ListViewGadget.
     If EventGadgetID()=0
      GetSelectedItems(0)
     EndIf
    ; Deselect all items whenever a right-click occurs on the ListViewGadget.
    Case #WM_RBUTTONDOWN
     If ChildWindowFromPoint_(WindowID(), WindowMouseX(), WindowMouseY())=GadgetID(0)
       SendMessage_(GadgetID(0), #LB_SETSEL, #False, -1)
     EndIf
    Case #PB_Event_CloseWindow
     Close=1
   EndSelect
  Until Close=1
EndIf
End
Tranquil
Post Reply