Returning string from multiselect

Just starting out? Need help? Post your questions and find answers here.
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Returning string from multiselect

Post by npath »

I am trying to figure out how to return the string values from a multiselect on a listicon gadget. In the following sample code, I would like to select several, and then have the numerous selections displayed in the message requester:

Code: Select all

#Window_Main=1
#Gadget_Main_ListIcon=2
#Gadget_Main_Button=3

If OpenWindow(#Window_Main,177,164,262,277,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_WindowCentered,"MultiSelect Test")
  If CreateGadgetList(WindowID(#Window_Main))
      ListIconGadget(#Gadget_Main_ListIcon,10,10,240,230,"ListIcon Test",100,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection)
      AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 1")
      AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 2")
      AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 3")
      ButtonGadget(#Gadget_Main_Button,190,250,60,20,"OK")
  EndIf
  Repeat
    EventID=WaitWindowEvent()
    Select EventID
      Case #PB_Event_Gadget
        Select EventGadgetID()
          Case #Gadget_Main_Button
            selected$=GetGadgetText(#Gadget_Main_ListIcon)
            MessageRequester("Test","You selected "+selected$)
        EndSelect
    EndSelect
  Until EventID=#PB_Event_CloseWindow
  CloseWindow(#Window_Main)
EndIf
End
I am working on a program that generates diagnostic pathology reports. The idea is that the user selects from standard descriptions of pathology specimens. Mostly, I am able to use ListView gadgets with single selections. However, occasionally I need to have the user select from multiple choices, then return those choices to strings for incorporation into the final report. Any help would be greatly appreciated.

Npath
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

I would do something like this:

Code: Select all

#Window_Main=1
#Gadget_Main_ListIcon=2
#Gadget_Main_Button=3

If OpenWindow(#Window_Main,177,164,262,277,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_WindowCentered,"MultiSelect Test")
  If CreateGadgetList(WindowID(#Window_Main))
    ListIconGadget(#Gadget_Main_ListIcon,10,10,240,230,"ListIcon Test",100,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection)
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 1")
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 2")
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Test 3")
    ButtonGadget(#Gadget_Main_Button,190,250,60,20,"OK")
  EndIf
  Repeat
    EventID=WaitWindowEvent()
    Select EventID
      Case #PB_Event_Gadget
        Select EventGadgetID()
          Case #Gadget_Main_Button
            selected$ = ""
            For i = 0 To CountGadgetItems(#Gadget_Main_ListIcon)-1
              If GetGadgetItemState(#Gadget_Main_ListIcon,i) = #PB_ListIcon_Selected
                selected$ = selected$ + "Row " + Str(i+1) + Chr(10)
              EndIf
            Next 
            MessageRequester("Test","You selected "+ Chr(10) + selected$)
        EndSelect
    EndSelect
  Until EventID=#PB_Event_CloseWindow
  CloseWindow(#Window_Main)
EndIf
End 
But I bet there would be a better way...
-Beach
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

@Beach,

Thanks for the input. The only problem I am having is that I would like to return the actual strings from the choices. I suppose I could use logical arguments to set the row number to a string. The problem is, however, that with a large number of choices, this could be rather cumbersome. I will keep working at it.

Npath
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post by Beach »

Maybe I'm not following... :) You need the data in the cells? Would this work?

Code: Select all

#Window_Main=1
#Gadget_Main_ListIcon=2
#Gadget_Main_Button=3

If OpenWindow(#Window_Main,177,164,262,277,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered,"MultiSelect Test")
  If CreateGadgetList(WindowID(#Window_Main))
    ListIconGadget(#Gadget_Main_ListIcon,10,10,240,230,"ListIcon Test",100,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection)
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Atlanta")
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Nashville")
    AddGadgetItem(#Gadget_Main_ListIcon,-1,"Dallas")
    ButtonGadget(#Gadget_Main_Button,190,250,60,20,"OK")
  EndIf
  Repeat
    EventID=WaitWindowEvent()
    Select EventID
      Case #PB_Event_Gadget
        Select EventGadgetID()
          Case #Gadget_Main_Button
            selected$ = ""
            For i = 0 To CountGadgetItems(#Gadget_Main_ListIcon)-1
              If GetGadgetItemState(#Gadget_Main_ListIcon,i) = #PB_ListIcon_Selected
                selected$ = selected$ + "Row " + Str(i+1)  + "  Data: " + GetGadgetItemText(#Gadget_Main_ListIcon,i,0) + Chr(10)
              EndIf
            Next
            MessageRequester("Test","You selected "+ Chr(10) + selected$)
        EndSelect
    EndSelect
  Until EventID=#PB_Event_CloseWindow
  CloseWindow(#Window_Main)
EndIf
End
-Beach
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

Hey Beach,

Thanks! That's exactly what I was trying to do. Clearly I have a lot to learn.

Npath
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

For Windows apps, it appears to be faster for me if I use #LVM_GETSELECTEDCOUNT and #LVM_GETNEXTITEM. Selecting all 1000 items took 40ms here compared to 90 ms when using GetGadgetItemState. Do some tests of your own and see which is faster for you. ;)

Code: Select all

#LVM_GETSELECTIONMARK = #LVM_FIRST + 66

#Window_Main=1 
#Gadget_Main_ListIcon=2 
#Gadget_Main_Button=3 

If OpenWindow(#Window_Main,177,164,262,277,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered|#PB_Window_WindowCentered,"MultiSelect Test") 
  If CreateGadgetList(WindowID(#Window_Main)) 
    ListIconGadget(#Gadget_Main_ListIcon,10,10,240,230,"ListIcon Test",100,#PB_ListIcon_MultiSelect|#PB_ListIcon_AlwaysShowSelection) 
    For i = 0 To 999
      AddGadgetItem(#Gadget_Main_ListIcon,-1,"List Item : " + Str(i)) 
    Next i
    ButtonGadget(#Gadget_Main_Button,190,250,60,20,"OK") 
  EndIf 
  Repeat 
    EventID=WaitWindowEvent() 
    Select EventID 
      Case #PB_Event_Gadget 
        Select EventGadgetID() 
          Case #Gadget_Main_Button 
            selected$ = ""
            ; --> Get the total number of items selected
            totalItemsSelected = SendMessage_(GadgetID(#Gadget_Main_ListIcon), #LVM_GETSELECTEDCOUNT, 0, 0)
            ; --> We'll start searching from the beginning
            currentItem = -1
            ; --> Loop through the ListIconGadget looking for selected items
            ; --> As soon as we have collected all selected items, we're done
            StartTime = ElapsedMilliseconds()
            For i = 1 To totalItemsSelected
              thisSelectedItem = SendMessage_(GadgetID(#Gadget_Main_ListIcon), #LVM_GETNEXTITEM, currentItem, #LVNI_ALL | #LVNI_SELECTED)
              ; --> Get the selected item text
              selected$ + GetGadgetItemText(#Gadget_Main_ListIcon, thisSelectedItem, 0) + #CRLF$
              ; --> move on to next item
              currentItem = thisSelectedItem
            Next i
            ElapsedTime = ElapsedMilliseconds() - StartTime
            MessageRequester("It took " + Str(ElapsedTime) + " milliseconds", "You selected " + Str(totalItemsSelected) + " items." + #CRLF$ + selected$) 
        EndSelect 
    EndSelect 
  Until EventID=#PB_Event_CloseWindow 
  CloseWindow(#Window_Main) 
EndIf 
End 
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
npath
User
User
Posts: 74
Joined: Tue Feb 15, 2005 5:15 pm

Post by npath »

Hey Sparkie,

Thanks! The windows API approach is quite fast.

Npath
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Your welcome npath :)

Shaving off a few milliseconds here and there never hurts. ;)
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post by newbie »

Very good piece of code, deserves a bump ;)
- Registered PB user -

Using PB 4.00
Post Reply