Page 1 of 1

Checkbox / ListIcon Example

Posted: Sun Dec 30, 2007 4:06 am
by Rook Zimbabwe
Since there wasn't an example in the docs, I made an example.

This allows you to check a checkbox and check to see how many are checked... You can also force all boxes checked. And you can see how boxes can be unchecked.

PB 4.1 code { new format }

Code: Select all

; PB 4.1 version...
; Rook Zimbabwe 29 December 2007
;
Enumeration
  #Window_0
EndEnumeration

Enumeration
  #Listview_0
  #Button_0
  #Button_1
  #ListIcon_0
EndEnumeration

Structure VisualDesignerGadgets
  Gadget.l
  EventFunction.l
EndStructure

Global NewList EventProcedures.VisualDesignerGadgets()

Procedure Listview_0_Event(Window, Event, Gadget, Type)
  Debug "#Listview_0"
EndProcedure

Procedure Button_0_Event(Window, Event, Gadget, Type)
  Debug "#Button_0"
  result = CountGadgetItems(#Listicon_0)
    For c = 0 To result
      h = GetGadgetItemState(#Listicon_0,c)
        If h = #PB_ListIcon_Checked
          junk = junk + 1
            SetGadgetItemState(#Listicon_0, c, 0) ; reset checked status (CLEANITUP!!!)
       EndIf
    Next
    j$ = Str(junk)
    golf$ = "CHECKED: "+j$
      AddGadgetItem(#Listview_0,-1, golf$)  
EndProcedure

Procedure Button_1_Event(Window, Event, Gadget, Type)
  Debug "#Button_1"
  result = CountGadgetItems(#Listicon_0)
    For c = 0 To result
            SetGadgetItemState(#Listicon_0, c, #PB_ListIcon_Checked ) ; Force boxes checked... using " 1 " will NOT do it
    Next
    golf$ = " ALL ITEMS FORCE CHECKED!"
      AddGadgetItem(#Listview_0,-1, golf$)
EndProcedure

Procedure ListIcon_0_Event(Window, Event, Gadget, Type)
  Debug "#ListIcon_0"
EndProcedure

Procedure RegisterGadgetEvent(Gadget, *Function)
  
  If IsGadget(Gadget)
    AddElement(EventProcedures())
    EventProcedures()\Gadget        = Gadget
    EventProcedures()\EventFunction = *Function
  EndIf
  
EndProcedure

Procedure CallEventFunction(Window, Event, Gadget, Type)
  
  ForEach EventProcedures()
    If EventProcedures()\Gadget = Gadget
      CallFunctionFast(EventProcedures()\EventFunction, Window, Event, Gadget, Type)
      LastElement(EventProcedures())
    EndIf
  Next
  
EndProcedure

Procedure Open_Window_0()
  
  If OpenWindow(#Window_0, 5, 5, 400, 200, "CHECKBOX EXAMPLE",  #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
    If CreateGadgetList(WindowID(#Window_0))
      ListIconGadget(#ListIcon_0, 12, 6, 180, 126, "Column1", 160, #PB_ListIcon_CheckBoxes)
      RegisterGadgetEvent(#ListIcon_0, @ListIcon_0_Event())
      ButtonGadget(#Button_0, 12, 138, 180, 36, "GET GADGET STATE")
      RegisterGadgetEvent(#Button_0, @Button_0_Event())
      ButtonGadget(#Button_1, 207, 138, 180, 36, "CHECK ALL")
      RegisterGadgetEvent(#Button_1, @Button_1_Event())
      ListViewGadget(#Listview_0, 207, 6, 180, 126)
      RegisterGadgetEvent(#Listview_0, @Listview_0_Event())
      
    EndIf
  EndIf
EndProcedure

Open_Window_0()

For x = 0 To 11
  xx$ = Str(x)
  AddGadgetItem(#ListIcon_0, -1, "LINE"+xx$)
Next

result = CountGadgetItems(#Listicon_0)
r$ = Str(result)
cough$ = "TOTAL ITEMS: "+r$
AddGadgetItem(#Listview_0, -1, cough$)

Repeat
  
  Event  = WaitWindowEvent()
  Gadget = EventGadget()
  Type   = EventType()
  Window = EventWindow()
  
  Select Event
    Case #PB_Event_Gadget
      CallEventFunction(Window, Event, Gadget, Type)
      
  EndSelect
  
Until Event = #PB_Event_CloseWindow

End
I hope this helps someone! :D

Posted: Sun Dec 30, 2007 8:37 am
by electrochrisso
Thanks for sharing Rook!
Could come in handy one day. :D