Why 2 change events from ListIcon?

Just starting out? Need help? Post your questions and find answers here.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Why 2 change events from ListIcon?

Post by tua »

Why do I get 2 events - no matter whether I click or use navigation keys in the listicon?

Code: Select all

Procedure LI_Change()
  Debug "Change"
EndProcedure

If OpenWindow(0, 100, 100, 300, 300, "Test")
  li = ListIconGadget(#PB_Any, 10, 10, 280, 280, "ID", 50, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(li, 1, "Name", 210)
  AddGadgetItem(li, -1, Str(15)  + #LF$ + "John Doe")
  AddGadgetItem(li, -1, Str(237) + #LF$ + "Jane Doe")
  AddGadgetItem(li, -1, Str(172) + #LF$ + "Baby Doe")
  ;
  BindGadgetEvent(li, @LI_Change(), #PB_EventType_Change)
EndIf

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Why 2 change events from ListIcon?

Post by Caronte3D »

Bug? :?
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Why 2 change events from ListIcon?

Post by jacdelad »

Not always: No item selected -> select 1 item => one "change".
Windows 10 x64

Also interesting: It fires only once within the event loop:

Code: Select all

Procedure LI_Change()
  Debug "Change"
EndProcedure

If OpenWindow(0, 100, 100, 300, 300, "Test")
  li = ListIconGadget(#PB_Any, 10, 10, 280, 280, "ID", 50, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(li, 1, "Name", 210)
  AddGadgetItem(li, -1, Str(15)  + #LF$ + "John Doe")
  AddGadgetItem(li, -1, Str(237) + #LF$ + "Jane Doe")
  AddGadgetItem(li, -1, Str(172) + #LF$ + "Baby Doe")
  ;
  BindGadgetEvent(li, @LI_Change(), #PB_EventType_Change)
EndIf

Repeat:
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventType()
        Case #PB_EventType_Change
          If EventGadget()=li
            Debug "Change in loop"
          EndIf
      EndSelect
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Why 2 change events from ListIcon?

Post by ChrisR »

The first event is to deselect the current item and the second to select the new item.
Is it normal to have an event for deselected or should it be as in eventloop with a single event ?

Code: Select all

Procedure LI_Change()
  ; #PB_EventType_Change = 768
  ; Selected item or -1 if there is no item selected
  Debug "EventType=" + Str(EventType()) + " - Selected Item=" + Str(GetGadgetState(EventGadget()))
EndProcedure

If OpenWindow(0, 100, 100, 300, 300, "Test")
  li = ListIconGadget(#PB_Any, 10, 10, 280, 280, "ID", 50, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(li, 1, "Name", 210)
  AddGadgetItem(li, -1, Str(15)  + #LF$ + "John Doe")
  AddGadgetItem(li, -1, Str(237) + #LF$ + "Jane Doe")
  AddGadgetItem(li, -1, Str(172) + #LF$ + "Baby Doe")
  ;
  BindGadgetEvent(li, @LI_Change(), #PB_EventType_Change)
EndIf

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Why 2 change events from ListIcon?

Post by tua »

I do consider this a bug because the 'deselect' event does not provide any additional info, such as 'what's the item that we just left' - GetGadgetState() always returns -1 for it - at least I can now use the GetGadgetState() test for - 1 to suppress the unwanted extra event.
User avatar
jacdelad
Addict
Addict
Posts: 2032
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Why 2 change events from ListIcon?

Post by jacdelad »

After a change event you always should use GetGadgetState to determine if there's an element selected at all. Whatever you want to do after the change, no selected element will be a special case.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Axolotl
Addict
Addict
Posts: 873
Joined: Wed Dec 31, 2008 3:36 pm

Re: Why 2 change events from ListIcon?

Post by Axolotl »

Hi tua,
If you think it is a bug, then you have to report it as a bug report. I think it will then be decided by the chief developers.
Alternatively, this would be a solution to react to the different events.

Code: Select all

Procedure LI_Change()
  Static s_item.i = -1 
  Protected item.i 
  ; #PB_EventType_Change = 768
  ; Selected item or -1 if there is no item selected
  item = GetGadgetState(EventGadget()) 

  If item = -1  ; Event -> Deselect 
    If s_item > -1 
      Debug "do something with the deselected item = " + s_item ; use the kept item 
      ; 
    EndIf 
  Else ; Event -> Select 
      Debug "do something with the selected item   = " + item   ; use the current item 
    ;
  EndIf 
  s_item = item   ; keep the (de) selection for the next event  
EndProcedure

If OpenWindow(0, 100, 100, 300, 300, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  li = ListIconGadget(#PB_Any, 10, 10, 280, 280, "ID", 50, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect | #PB_ListIcon_AlwaysShowSelection)
  AddGadgetColumn(li, 1, "Name", 210)
  AddGadgetItem(li, -1, Str(15)  + #LF$ + "John Doe")
  AddGadgetItem(li, -1, Str(237) + #LF$ + "Jane Doe")
  AddGadgetItem(li, -1, Str(172) + #LF$ + "Baby Doe")
  ;
  BindGadgetEvent(li, @LI_Change(), #PB_EventType_Change)
EndIf

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
Happy coding and stay healthy.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
the.weavster
Addict
Addict
Posts: 1581
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: Why 2 change events from ListIcon?

Post by the.weavster »

If you're not too far into your project you might want to look at Thorsten1867's GridEXModule as an alternative to ListIconGadget()
Post Reply