GetGadgetItemState and ListIconGadget

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

GetGadgetItemState and ListIconGadget

Post by jak64 »

Hello,
I can't get the number of the line I checked (just by checking the checkbox, without clicking on the name of the element (AAAAA or BBBB, etc.)!

Thanks for your help

Code: Select all

EnableExplicit

Global fenetre.i
Global event.i
Global liste_icon.i
Global i.i
Global resultat.i

fenetre = OpenWindow(#PB_Any, 0, 0, 800,600, "Test ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
liste_icon = ListIconGadget(#PB_Any, 10,10, 160, 550, "TEST", 150, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect)
For i = 0 To 25
  AddGadgetItem(liste_icon, -1, RSet(Chr(i + 65), 8, Chr(i + 65)))
Next i

Repeat
  event = WaitWindowEvent()
  
  Select event
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case liste_icon
          Select EventType()
                      
            Case #PB_EventType_LeftClick
              Debug GetGadgetItemState(liste_icon, -1) & #PB_ListIcon_CheckBoxes
              resultat = GetGadgetState(liste_icon)
              If resultat > -1
                Debug GetGadgetText(liste_icon)
              EndIf
          EndSelect
          
      EndSelect
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: GetGadgetItemState and ListIconGadget

Post by NicTheQuick »

if you want to check for the checkbox state you have to define which entry you wanna check. So first get the entry you clicked on and then use `GetGadgetItemState()` to check for the Checkbox state.

Code: Select all

							resultat = GetGadgetState(liste_icon)
							If resultat > -1
								Debug GetGadgetItemState(liste_icon, resultat) & #PB_ListIcon_CheckBoxes
								Debug GetGadgetText(liste_icon)
							EndIf
And if you want to get all the checkbox states you need to iterate over all the entries.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: GetGadgetItemState and ListIconGadget

Post by NicTheQuick »

Maybe I misunderstood your issue. But on Linux when I click on just a checkbox the underlying entry will be selected too. If this is different on Windows, I unfortunately can not help.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: GetGadgetItemState and ListIconGadget

Post by jak64 »

Hello NicTheQuick,

I understood that but that's exactly what I don't want: I want to ONLY check the box to the left of the item, without left-clicking on the item.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: GetGadgetItemState and ListIconGadget

Post by jak64 »

Anyway, it doesn't work, whether I check or uncheck the checkbox, I always get "yes".
On the other hand, if I click on the item, for example BBBB (without checking the box), I also get "yes"!!!

Code: Select all

EnableExplicit

Global fenetre.i
Global event.i
Global liste_icon.i
Global i.i
Global resultat.i

fenetre = OpenWindow(#PB_Any, 0, 0, 800,600, "Test ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
liste_icon = ListIconGadget(#PB_Any, 10,10, 160, 550, "TEST", 150, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect)
For i = 0 To 25
  AddGadgetItem(liste_icon, -1, RSet(Chr(i + 65), 8, Chr(i + 65)))
Next i

Repeat
  event = WaitWindowEvent()
  
  Select event
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case liste_icon
          Select EventType()
              
            Case #PB_EventType_LeftClick
              resultat = GetGadgetState(liste_icon)
              If resultat > -1
                If GetGadgetItemState(liste_icon, resultat) & #PB_ListIcon_CheckBoxes
                  Debug "yes"
                Else
                  Debug "no"
                EndIf
              EndIf
          EndSelect
          
      EndSelect
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow
miso
Enthusiast
Enthusiast
Posts: 491
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: GetGadgetItemState and ListIconGadget

Post by miso »

I dare to assume a bug, where on windows7, the #PB_ListIcon_FullRowSelect is not working correctly. I'm not expert though, I may be wrong.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: GetGadgetItemState and ListIconGadget

Post by jak64 »

hello miso,

I'm on Windows 11
User avatar
Caronte3D
Addict
Addict
Posts: 1371
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: GetGadgetItemState and ListIconGadget

Post by Caronte3D »

It seems a bug to me.

Anyway here is a workaround for cases where click only on the check:

Code: Select all

EnableExplicit

Global fenetre.i
Global event.i
Global liste_icon.i
Global i.i
Global resultat.i=-1

fenetre = OpenWindow(#PB_Any, 0, 0, 800,600, "Test ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
liste_icon = ListIconGadget(#PB_Any, 10,10, 160, 550, "TEST", 150, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect)
For i = 0 To 25
  AddGadgetItem(liste_icon, -1, RSet(Chr(i + 65), 8, Chr(i + 65)))
  SetGadgetItemData(liste_icon,i,0)
Next i

Repeat
  event = WaitWindowEvent()
  
  Select event
      
    Case #PB_Event_Gadget
      Select EventGadget()
          
        Case liste_icon
          Select EventType()
                      
            Case #PB_EventType_Change
              For i = 0 To 25
                ;Debug GetGadgetItemData(liste_icon,i)
                If GetGadgetItemData(liste_icon,i)<>Bool(GetGadgetItemState(liste_icon, i) & #PB_ListIcon_Checked)
                  SetGadgetItemData(liste_icon,i,Bool(GetGadgetItemState(liste_icon, i) & #PB_ListIcon_Checked ))
                  resultat=i
                  Break
                EndIf
              Next i

							If resultat > -1
							  SetGadgetState(liste_icon, resultat)
							  Debug "Check: "+GetGadgetItemData(liste_icon, resultat)
								Debug "Text:"+GetGadgetText(liste_icon)
							EndIf
							resultat=-1
          EndSelect
          
      EndSelect
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: GetGadgetItemState and ListIconGadget

Post by jak64 »

Hello Caronte3D,

I had thought about it but it is still a "heavy" solution.

It's a shame that we have to go through this...
miso
Enthusiast
Enthusiast
Posts: 491
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: GetGadgetItemState and ListIconGadget

Post by miso »

Hello jak64!

Then 2 type of Windows has been tested. Might worth a bug report.
I was fiddling with your example, and I choosed to stop. I found multiple problems regarding to listicongadget. Just for one example I could not get #pb_eventtype_columnclick from eventtype(). After that #pb_listicon_ClickedColumn with GetGadgetAttribute would help, but I could not get those.

As I said, I may be wrong, these gadget things are not on my daily focus, it's not my strong side.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: GetGadgetItemState and ListIconGadget

Post by BarryG »

It's not a bug to click a ListIconGadget checkbox and NOT also have its item selected, if that's what some of you are saying? It's normal behavior and has uses. Don't break my app by reporting this incorrectly as a bug.

@Jak64: I think you want to do something like this? -> https://www.purebasic.fr/english/viewto ... 50#p550650
miso
Enthusiast
Enthusiast
Posts: 491
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: GetGadgetItemState and ListIconGadget

Post by miso »

Please don't be upset. We are asking the experts in the coding questions section. I mean no harm.
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: GetGadgetItemState and ListIconGadget

Post by mk-soft »

In Windows you can mark a checkbox in the list without the selected item changing.
On macOS and Linux, the selection item is changed. This is OS specific.

Whether a checkbox is marked in a list, you have to check all entries.

Update
Not LeftClick evaluated, but Change, then works via keyboard too

Code: Select all

;-TOP

EnableExplicit

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainList
  #MainButton
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainList, 0, 0, dx, dy - 40)
  ResizeGadget(#MainButton, 5, dy - 35, 120, 25)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  Protected StateItem, LastStateItem
  Protected CheckedItem, i
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ListIconGadget(#MainList, 0, 0, dx, dy - 40, "Column 0", 400, #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_MultiSelect)
    ButtonGadget(#MainButton, 5, dy - 35, 120, 25, "Ok")
    For i = 0 To 9
      AddGadgetItem(#MainList, -1 , "Item " + i)
    Next
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainList
              Select EventType()
                Case #PB_EventType_Change
                  For i = 0 To CountGadgetItems(#MainList) - 1
                    StateItem = GetGadgetItemState(#MainList, i)
                    LastStateItem = GetGadgetItemData(#MainList, i)
                    If StateItem <> LastStateItem
                      If (StateItem & #PB_ListIcon_Checked) <> (LastStateItem & #PB_ListIcon_Checked)
                        Debug "Change Checked Item " + i + " State " + Bool(StateItem & #PB_ListIcon_Checked)
                      EndIf
                      If (StateItem & #PB_ListIcon_Selected) <> (LastStateItem & #PB_ListIcon_Selected)
                        Debug "Change Selected Item " + i + " State " + Bool(StateItem & #PB_ListIcon_Selected)
                      EndIf
                      SetGadgetItemData(#MainList, i, StateItem)
                    EndIf
                  Next
                    
              EndSelect
              
            Case #MainButton
              For i = 0 To CountGadgetItems(#MainList) - 1
                CheckedItem = GetGadgetItemState(#MainList, i) & #PB_ListIcon_Checked
                Debug "Checked Item " + i + " State " + Bool(CheckedItem = #PB_ListIcon_Checked)
              Next
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: GetGadgetItemState and ListIconGadget

Post by jak64 »

Hello BarryG,

Indeed, I tested this code and this is what I want to do:

I have a list of several hundred TV series and I would like, by checking the box, to put the item in green to indicate that I have already seen it (I also type the series in an sqlite database) and I will have 2 boxes to check, above the list, to choose, series seen and series not seen.
miso
Enthusiast
Enthusiast
Posts: 491
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: GetGadgetItemState and ListIconGadget

Post by miso »

Thanks mk-soft, I always learn something from you.
Post Reply