ListIconGadget with checkboxes

Just starting out? Need help? Post your questions and find answers here.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

ListIconGadget with checkboxes

Post by SFSxOI »

I don't normally use the ListIconGadget so i'm not familiar with its quirks. If you have a list icon gadget using multiple checkboxes like the below example:

Code: Select all

If OpenWindow(0, 0, 0, 640, 300, "ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget    (7,  10, 105, 300, 20, "ListIcon with Checkbox", #PB_Text_Center)
    ListIconGadget(1,  10, 120, 340, 80, "Column 1", 100, #PB_ListIcon_CheckBoxes)  ; ListIcon with checkboxs
    For b = 2 To 4
      AddGadgetColumn(1, b, "Column " + Str(b), 65)
    Next
    For b = 0 To 2
      AddGadgetItem(1, b, "Item 1"+Chr(10)+"Item 2"+Chr(10)+"Item 3"+Chr(10)+"Item 4")
    Next
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
How do you ensure that only one item can be/remain checked at a time and once an item is checked any other item that was previously checked is automatically unchecked and the currently checked item remains checked until another item is checked?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: ListIconGadget with checkboxes

Post by RASHAD »

@SFSxOI Hi
I think I posted something similar before

Code: Select all


If OpenWindow(0, 0, 0, 640, 400, "ListIconGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  ListIconGadget(1,  10, 10, 620,380, "Column 0", 100, #PB_ListIcon_GridLines|#PB_ListIcon_CheckBoxes|#PB_ListIcon_FullRowSelect)
 
  For c = 1 To 6
    AddGadgetColumn(1, c, "Column " + Str(c), 100)
  Next
  For r = 0 To 100
    AddGadgetItem(1, r, "  Item "+Str(r)+Chr(10)+"Item "+Str(r)+Chr(10)+"Item 3"+Chr(10)+"Item 4")
  Next
EndIf

Repeat
  Select WaitWindowEvent(1)
     
    Case #PB_Event_CloseWindow
      Quit = 1
     
    Case #PB_Event_Gadget
      Select EventGadget()               
        Case 1
          Select EventType()            
                   
            Case #PB_EventType_LeftClick,#PB_EventType_RightClick
                If Old > 0 Or GetGadgetState(1) > 0              
                   SetGadgetItemState(1,GetGadgetState(1),2)
                EndIf
                For x = 0 To CountGadgetItems(1)
                  If GetGadgetItemState(1, x) = 2
                    If x <> Old
                      SetGadgetItemState(1,Old,0)
                      Break
                    EndIf
                  EndIf
                Next
                
              For x = 0 To CountGadgetItems(1)
                If GetGadgetItemState(1, x) = 2
                  SetGadgetState(1,x)
                  Old = x
                  Break
                EndIf
              Next                      
                   
             
          EndSelect
         
      EndSelect
  EndSelect
 
Until Quit = 1

Last edited by RASHAD on Mon Feb 11, 2013 5:08 pm, edited 1 time in total.
Egypt my love
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: ListIconGadget with checkboxes

Post by srod »

Not sure Rashad's does what you are after?

Here's one :

Code: Select all

If OpenWindow(0, 0, 0, 640, 300, "ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    TextGadget    (7,  10, 105, 300, 20, "ListIcon with Checkbox", #PB_Text_Center)
    ListIconGadget(1,  10, 120, 340, 80, "Column 1", 100, #PB_ListIcon_CheckBoxes)  ; ListIcon with checkboxs
    For b = 2 To 4
      AddGadgetColumn(1, b, "Column " + Str(b), 65)
    Next
    For b = 0 To 2
      AddGadgetItem(1, b, "Item 1"+Chr(10)+"Item 2"+Chr(10)+"Item 3"+Chr(10)+"Item 4")
    Next
    lastChecked = -1
    Repeat
      eventID = WaitWindowEvent()
      Select eventID
        Case #PB_Event_Gadget
          If EventType() = #PB_EventType_Change
            numItems = CountGadgetItems(1)
            countChecks = 0
            For i = 0 To numItems-1
              state = GetGadgetItemState(1, i)
              If state & #PB_ListIcon_Checked
                countChecks+1
                If i <> lastChecked
                  If lastChecked <> -1
                    SetGadgetItemState(1, lastChecked, GetGadgetItemState(1, lastChecked) ! #PB_ListIcon_Checked)
                  EndIf
                  lastChecked = i
                  Break
                EndIf
              EndIf
            Next
            If countChecks = 0
              lastChecked = -1
            EndIf
          EndIf
      EndSelect
    Until eventID = #PB_Event_CloseWindow
  EndIf
I may look like a mule, but I'm not a complete ass.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4947
Joined: Sun Apr 12, 2009 6:27 am

Re: ListIconGadget with checkboxes

Post by RASHAD »

Well srod :)
I think you hit the case which I did not consider
Previous post modified
And next is one extra snippet

Code: Select all

Global Row

Procedure WndProc(hwnd, uMsg, wParam, lParam)
    Select uMsg
      Case #WM_NOTIFY
               *uMsg.NMHDR = lParam                                                                     
              If *uMsg\hwndFrom = GadgetID(1) And *uMsg\code = #NM_CLICK
                 *Li.NMITEMACTIVATE = lParam
                 Row = *Li\iItem
              EndIf

    EndSelect
   ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure


If OpenWindow(0, 0, 0, 640, 400, "ListIconGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
 
  ListIconGadget(1,  10, 10, 620,380, "Column 0", 100, #PB_ListIcon_GridLines|#PB_ListIcon_CheckBoxes|#PB_ListIcon_FullRowSelect)
 
  For c = 1 To 6
    AddGadgetColumn(1, c, "Column " + Str(c), 100)
  Next
  For r = 0 To 100
    AddGadgetItem(1, r, "  Item "+Str(r)+Chr(10)+"Item "+Str(r)+Chr(10)+"Item 3"+Chr(10)+"Item 4")
  Next
EndIf

 SetWindowCallback(@WndProc())

Repeat
  Select WaitWindowEvent(1)
     
    Case #PB_Event_CloseWindow
      Quit = 1
     
    Case #PB_Event_Gadget
      Select EventGadget()               
        Case 1
          Select EventType()                   
            Case #PB_EventType_LeftClick
            If Row <> Old
                SetGadgetItemState(1,Old,0)
                SetGadgetItemState(1,Row,3)
                Old = Row
            EndIf                   
             
          EndSelect
         
      EndSelect
  EndSelect
 
Until Quit = 1
Egypt my love
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: ListIconGadget with checkboxes

Post by SFSxOI »

RASHAD and srod;

Thank you both for your replies.

Tried both of the fine examples offered. srod, I ended up using yours as it fit perfectly into what i'm working on. RASHAD, gonna keep yours around as I think it may work out for another project i'm planning.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
!ns0
New User
New User
Posts: 4
Joined: Wed Feb 20, 2013 10:07 am

Re: ListIconGadget with checkboxes

Post by !ns0 »

srod, first of all thank you for your solution :)
But what to do if checkbox was checked from the beginning?

ImageImageImageImageImagesome time later ImageImageImageImageImage

:idea:

Code: Select all

If OpenWindow(0, 0, 0, 640, 300, "ListIconGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TextGadget    (7,  10, 105, 300, 20, "ListIcon with Checkbox", #PB_Text_Center)
  ListIconGadget(1,  10, 120, 340, 80, "Column 1", 100, #PB_ListIcon_CheckBoxes)  ; ListIcon with checkboxs
  For b = 2 To 4
    AddGadgetColumn(1, b, "Column " + Str(b), 65)
  Next
  For b = 0 To 2
    AddGadgetItem(1, b, "Item 1"+Chr(10)+"Item 2"+Chr(10)+"Item 3"+Chr(10)+"Item 4")
  Next
  
  SetGadgetItemState(1,0,#PB_ListIcon_Checked)  ;Check Item
  
  ;Get Checked Item
  lsItems = CountGadgetItems(1)
  For lastChecked = lsItems - 1 To 0 Step -1
    ChkResult = GetGadgetItemState(1, lastChecked)
    If ChkResult & #PB_ListIcon_Checked
      Break  
    EndIf     
  Next
  
  Repeat
    eventID = WaitWindowEvent()
    Select eventID
      Case #PB_Event_Gadget
        If EventType() = #PB_EventType_Change
          numItems = CountGadgetItems(1)
          countChecks = 0
          For i = 0 To numItems-1
            state = GetGadgetItemState(1, i)
            If state & #PB_ListIcon_Checked
              countChecks+1
              If i <> lastChecked
                If lastChecked <> -1
                  SetGadgetItemState(1, lastChecked, GetGadgetItemState(1, lastChecked) ! #PB_ListIcon_Checked)
                EndIf
                lastChecked = i
                Break
              EndIf
            EndIf
          Next
          If countChecks = 0
            lastChecked = -1
          EndIf
        EndIf
    EndSelect
  Until eventID = #PB_Event_CloseWindow
EndIf
▌│█║▌║▌ █║║▌║█
Post Reply