Page 1 of 1
Returning string from multiselect
Posted: Fri Apr 08, 2005 3:54 am
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
Posted: Fri Apr 08, 2005 4:00 am
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...
Posted: Fri Apr 08, 2005 8:59 pm
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
Posted: Fri Apr 08, 2005 9:13 pm
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
Posted: Sat Apr 09, 2005 12:47 am
by npath
Hey Beach,
Thanks! That's exactly what I was trying to do. Clearly I have a lot to learn.
Npath
Posted: Sat Apr 09, 2005 12:52 am
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
Posted: Sat Apr 09, 2005 1:08 am
by npath
Hey Sparkie,
Thanks! The windows API approach is quite fast.
Npath
Posted: Sat Apr 09, 2005 1:16 am
by Sparkie
Your welcome npath
Shaving off a few milliseconds here and there never hurts.

Posted: Mon Sep 04, 2006 3:25 pm
by newbie
Very good piece of code, deserves a bump
