Selecting ComboBox item by it's underlying data, not by row

Just starting out? Need help? Post your questions and find answers here.
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Selecting ComboBox item by it's underlying data, not by row

Post by SparrowhawkMMU »

Hi,

How can I programmatically set the gadget state of a combo box based not on the ordinal position of the item's row but on the value of the item's underlying data?

i.e.: I have a combo box showing Status descriptions which are fetched from a an HTTPRequest call to a web service API

For example:
Row 0 has text "Draft" and underlying data of 1
Row 1 has text "Staging" and underlying data of 2
Row 2 has text "Live" and underlying data of 3
Row 3 has text "Stopped" and underlying data of 4
Row 4 has text "Amended" and underlying data of 5

As far as I can tell, I can only use SetGadgetState() to set the selected row based on its position (eg row 0, row 1, row 99 etc). There does not appear to be a keyword to set the selected row based on the underlying data - is this correct? Do I need to loop through the combo box each time and compare the data to the value I want to set it to?

Note: the data may change so I do not want to set any constants or use other hard coding at the SpiderBasic end of things. So for example I cannot make the assumption that the row number I want is Status ID - 1 .

Thanks in advance for any help :)
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Selecting ComboBox item by it's underlying data, not by

Post by Peter »

perhaps this is a acceptable way for you:

Code: Select all

EnableExplicit

#Window = 0
#ComboBoxGadget = 0

Procedure SetGadgetStateByItemData(Gadget, GadgetItemData)
  
  Protected Counter, CountedItems
  
  CountedItems = CountGadgetItems(Gadget) - 1
  
  For Counter = 0 To CountedItems
    If GetGadgetItemData(Gadget, Counter) = GadgetItemData
      SetGadgetState(Gadget, Counter)
      Break
    EndIf
  Next
  
EndProcedure

OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 300, 100, "SetGadgetStateByItemData()", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)

ComboBoxGadget(#ComboBoxGadget, 10, 10, 150, 20)

AddGadgetItem(#ComboBoxGadget, -1, "Draft")
SetGadgetItemData(#ComboBoxGadget, CountGadgetItems(#ComboBoxGadget) - 1, 1) ; underlying data of 1

AddGadgetItem(#ComboBoxGadget, -1, "Staging")
SetGadgetItemData(#ComboBoxGadget, CountGadgetItems(#ComboBoxGadget) - 1, 2) ; underlying data of 2

AddGadgetItem(#ComboBoxGadget, -1, "Live")
SetGadgetItemData(#ComboBoxGadget, CountGadgetItems(#ComboBoxGadget) - 1, 3) ; underlying data of 3

AddGadgetItem(#ComboBoxGadget, -1, "Stopped")
SetGadgetItemData(#ComboBoxGadget, CountGadgetItems(#ComboBoxGadget) - 1, 4) ; underlying data of 4

AddGadgetItem(#ComboBoxGadget, -1, "Amended")
SetGadgetItemData(#ComboBoxGadget, CountGadgetItems(#ComboBoxGadget) - 1, 5) ; underlying data of 5

SetGadgetStateByItemData(#ComboBoxGadget, 3) ; should select 'Live'
Greetings ... Peter
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Selecting ComboBox item by it's underlying data, not by

Post by SparrowhawkMMU »

Hi Peter,

Thanks for kindly taking the time to write some sample code - this is pretty much what I have resorted to already, but I was hoping that SB might have a command that I was not aware of to identify a row by its data. Looks like the answer is no.

I might raise a feature request, as lookup by hidden key in combos / lists would be very useful for CRUD type apps with lots of reference data (which is basically what I am writing right now)

Except that suddenly I cannot get ANY data into my combos, but that's a different issue - I'll probably raise a plea for help later on if I cannot get that working...

My day job is databases/backend/API/WebService coding. Having to think in "Front End" is proving quite a challenge for me sometimes :D
User avatar
SparrowhawkMMU
Posts: 291
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Selecting ComboBox item by it's underlying data, not by

Post by SparrowhawkMMU »

The non-populating combo boxes were due to the StickyWindow() command - seems that if this is issued then combo boxes do no work. I have raised a bug report for this.
Post Reply