Detecting ListIconGadget checkbox state

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Detecting ListIconGadget checkbox state

Post by PB »

Code updated for 5.20+

I just realised that checking for a value of #PB_ListIcon_Checked (which is 2)
with a ListIconGadget is not 100% reliable, because if the item is also selected,
then the return value is 3, not 2. You can check this by playing with the code
below. So, when you need to test the checkbox state of a ListIconGadget item,
make sure your test is whether the return value is >1, because:

0 = The item is unselected and unchecked.
1 = The item is selected.
2 = The item is checked.
3 = The item is checked and selected.

(@Fred: Maybe these values could go into the manual?).

Code: Select all

If OpenWindow(0, 350, 250, 270, 150, "ListIconGadget", #PB_Window_SystemMenu)
  ListIconGadget(0, 10, 10, 250, 115, "test", 200, #PB_ListIcon_CheckBoxes)
  For r =1 To 5 : AddGadgetItem(0, -1, "Item #" + Str(r)) : Next
  ButtonGadget(1, 10, 130, 200, 20," Click here to see all checkbox states")
  Repeat
    event = WaitWindowEvent()
    If event = #PB_Event_Gadget And EventGadget() = 1
      a$ = ""
      For r = 1 To 5
        item = GetGadgetState(0)
        a$ + "Item #" + Str(r - 1) + " = " + Str(GetGadgetItemState(0, r-1)) + Chr(13)
      Next
      MessageRequester("test", a$)
    EndIf
  Until event = #PB_Event_CloseWindow
EndIf
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

This is a normal behaviour I think, you have to test ItemState & #PB_ListIcon_Checked (instead of ItemState = #PB_ListIcon_Checked).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Yes, that's right.

Post by Fangbeast »

gnozal wrote:This is a normal behaviour I think, you have to test ItemState & #PB_ListIcon_Checked (instead of ItemState = #PB_ListIcon_Checked).
I use this one in my programs all the time.

& #PB_ListIcon_Checked
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Fred
Administrator
Administrator
Posts: 18253
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

There is an example in the doc, may be it's not really explicit (see GetGadgetItemState) ? Feel free to adjust it PB, and tell us :)
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Yeah I knew about using GetGadgetItemState, but I thought I could use it
like this: If GetGadgetItemState(#ListIcon,item)=#PB_ListIcon_Checked.
And when I was ticking/unticking things, the results sometimes worked and
sometimes didn't -- depending on if the ticked item was also highlighted.
So I was very confused for a long time, until I discovered why... so I posted
it here in case it helps others. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Phant0m``

Post by Phant0m`` »

The only real problem is the use of GetGadgetState, when clicking directly onto the checkbox, doesn’t return the line of the changed checkbox state. :wink:
Phant0m``

Post by Phant0m`` »

- ListIconGadget(): returns the first selected item index, -1 if none is selected
And so something like GetGadgetState(EventGadgetID()), would throw it off anyways, when clicking directly on the checkbox, and I don't see no native command to return the actual Line event. :/
Post Reply