Page 1 of 1

Detecting ListIconGadget checkbox state

Posted: Wed May 18, 2005 6:45 am
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

Posted: Wed May 18, 2005 8:10 am
by gnozal
This is a normal behaviour I think, you have to test ItemState & #PB_ListIcon_Checked (instead of ItemState = #PB_ListIcon_Checked).

Yes, that's right.

Posted: Wed May 18, 2005 10:06 am
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

Posted: Wed May 18, 2005 11:39 am
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 :)

Posted: Wed May 18, 2005 12:33 pm
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. :)

Posted: Wed May 18, 2005 1:39 pm
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:

Posted: Wed May 18, 2005 1:45 pm
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. :/