Page 1 of 1

How to Multiselect Checkboxes in ListIcon Gadget?

Posted: Tue Mar 17, 2009 10:57 pm
by rrpl
Hi,

I want to be able to multiselect the checkboxes in a ListIconGadget. Has anybody got any code examples for this.

I can multiselect the items OK, thats built into purebasic, but would like to be able to multiselect the checkboxes.

Any help would be greatly appreciated.

Posted: Tue Mar 17, 2009 11:40 pm
by srod
Do you mean check/uncheck the checkboxes using code?

Posted: Wed Mar 18, 2009 2:30 am
by rrpl
Yes thats right, to be able to check multiple boxes at once, say to click on the first checkbox then use shift-click on say the sixth box down and have all six boxes either checked or unchecked at once.

Posted: Wed Mar 18, 2009 10:51 am
by srod
You'll have to handle the mouse/keyboard messages yourself, but what you're looking for is SetGadgetItemState() with the #PB_ListIcon_Checked state flag.

Posted: Wed Mar 18, 2009 10:51 am
by rrpl
I have managed to create a solution for the above. As I did not find any previous examples of doing this, I have included a cut down example version for any other novices wanting to do the same thing.

Perhaps some of the more experienced out there may be able to improve and/or expand on it.

Code: Select all

; My example multiselect checkboxes

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #ListIcon_1
  #Text_1
  #Button_1
  #Button_2
EndEnumeration


Procedure SelectedPages()
  NumSelected=0
  For a=0 To CountGadgetItems(#ListIcon_1)-1
    Selected=GetGadgetItemState(#ListIcon_1, a)
    If Selected=#PB_ListIcon_Selected
      NumSelected=NumSelected+1
    EndIf
  Next
  ProcedureReturn NumSelected
EndProcedure

Procedure OpenWindow_Window_0()

  If OpenWindow(#Window_0, 249, 116, 450, 350, "Multiselect Checkboxes in ListIconGadget", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
   
    If CreateGadgetList(WindowID(#Window_0)) ;sorry still using PB ver 4.02
     ;Note: I think you need to remove the CreateGadgetList entry (above) for PB ver 4.30 
      ListIconGadget(#ListIcon_1,0,0,200,WindowHeight(#Window_0)-50,"Data To Extract",150,#PB_ListIcon_CheckBoxes|#PB_ListIcon_MultiSelect)
      TextGadget(#Text_1,220,50,200,200,"Example: Select Data 3 with mouse"+Chr(10)+"then use Shift mouse click on Data 12."+Chr(10)+"Note:Checkboxes Data 3 to Data 12 are now checked")
      ButtonGadget(#Button_1,5,WindowHeight(#Window_0)-35,150,25,"Process Selected")
      ButtonGadget(#Button_2,200,WindowHeight(#Window_0)-35,150,25,"Clear All Checkboxes")
      
      ; load some example data
      AddGadgetItem(#ListIcon_1,-1," Data 1")
      AddGadgetItem(#ListIcon_1,-1," Data 2")
      AddGadgetItem(#ListIcon_1,-1," Data 3")
      AddGadgetItem(#ListIcon_1,-1," Data 4")
      AddGadgetItem(#ListIcon_1,-1," Data 5")
      AddGadgetItem(#ListIcon_1,-1," Data 6")
      AddGadgetItem(#ListIcon_1,-1," Data 7")
      AddGadgetItem(#ListIcon_1,-1," Data 8")
      AddGadgetItem(#ListIcon_1,-1," Data 9")
      AddGadgetItem(#ListIcon_1,-1," Data 10")
      AddGadgetItem(#ListIcon_1,-1," Data 11")
      AddGadgetItem(#ListIcon_1,-1," Data 12")
      AddGadgetItem(#ListIcon_1,-1," Data 13")
      AddGadgetItem(#ListIcon_1,-1," Data 14")
      
    EndIf
    
  EndIf
EndProcedure

OpenWindow_Window_0()

Repeat
  
  Select WaitWindowEvent()

    Case #PB_Event_Gadget
      
      Select EventGadget ()
          
        Case #ListIcon_1
          Select EventType ()
            Case #PB_EventType_Change
              If SelectedPages()=1
                Tag=1
              EndIf
          EndSelect
          
        Case #Button_1
          Mess.s=""
          For a=0 To CountGadgetItems(#ListIcon_1)-1
            Selected=GetGadgetItemState(#ListIcon_1, a)
            If Selected=#PB_ListIcon_Checked
              If Mess=""
                Mess=Mess+GetGadgetItemText(#ListIcon_1,a,0)
              Else
                Mess=Mess+","+GetGadgetItemText(#ListIcon_1,a,0)
              EndIf
            EndIf
          Next
          MessageRequester("You selected the following",Mess)
          
        Case #Button_2
          For a=0 To CountGadgetItems(#ListIcon_1)-1
              SetGadgetItemState(#ListIcon_1,a,0)
          Next
          
  
      EndSelect

 
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect


If SelectedPages()>1 And Tag=1 ; Multiselect checkboxes
  Tag=0
  For a=0 To CountGadgetItems(#ListIcon_1)-1
    Selected=GetGadgetItemState(#ListIcon_1, a)
    If Selected=#PB_ListIcon_Selected
      SetGadgetItemState(#ListIcon_1,a,#PB_ListIcon_Checked)
    EndIf
  Next
EndIf

ForEver
The above code includes CreateGadgetList as I am still using PB ver 4.02
I think you need to remove this for PB ver 4.30

Posted: Wed Mar 18, 2009 12:18 pm
by yrreti
Kind of neat! Thanks for sharing.
I would suggest though that you would be better to upgrade to the latest PB version because you will be left behind.
I used to hold back after writing certain programs with older versions that wouldn't work when a newer version was
released too. But then almost all the new info and new examples reference the new release. I usually wait a little bit
while watching the Bug listing. But I also pay attention to some of the changes made. Then it is easier to correct
the older code I have. Otherwise, you'll find your self eventually really left behind. Beside the newer versions correct
some issues and often add many new and better features. Just my two cents.

Posted: Wed Mar 18, 2009 10:18 pm
by rrpl
I would suggest though that you would be better to upgrade to the latest PB version because you will be left behind.
Yeah on that subject Yrreti, do you know if the new version of PB still works OK with Windows 98? As some of the computers I work with still use this OS.

Posted: Thu Mar 19, 2009 12:18 pm
by yrreti
That is a good question? To be honest, I'm not sure. I would probably start
a new topic under Coding Question "eg: Will PB4.3 work ok under Win98?"
That way you should get some input from more experience ones.
That way you should probably get a better idea if it does and if there are
any problems. I would suspect some issues, but there maybe some work arounds.

Re: How to Multiselect Checkboxes in ListIcon Gadget?

Posted: Wed Feb 27, 2013 7:12 pm
by !ns0
rrpl, Thank you very much for your snip, but here one thing - when you process selected, last selected item drop out of the mess string, here is my simple workaround :D .
Hope this will be useful for someone :)

Code: Select all

;multiselect checkboxes #potential fix

Enumeration
  #Window_0
EndEnumeration

Enumeration
  #ListIcon_1
  #Text_1
  #Button_1
  #Button_2
EndEnumeration


Procedure SelectedPages()
  NumSelected=0
  For a=0 To CountGadgetItems(#ListIcon_1)-1
    Selected=GetGadgetItemState(#ListIcon_1, a)
    If Selected=#PB_ListIcon_Selected
      NumSelected=NumSelected+1
    EndIf
  Next
  ProcedureReturn NumSelected
EndProcedure

Procedure OpenWindow_Window_0()
  
  If OpenWindow(#Window_0, 249, 116, 450, 350, "Multiselect Checkboxes in ListIconGadget", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_TitleBar|#PB_Window_ScreenCentered)
    
    ListIconGadget(#ListIcon_1,0,0,200,WindowHeight(#Window_0)-50,"Data To Extract",150,#PB_ListIcon_CheckBoxes|#PB_ListIcon_MultiSelect)
    TextGadget(#Text_1,220,50,200,200,"Example: Select Data 3 with mouse"+Chr(10)+"then use Shift mouse click on Data 12."+Chr(10)+"Note:Checkboxes Data 3 to Data 12 are now checked")
    ButtonGadget(#Button_1,5,WindowHeight(#Window_0)-35,150,25,"Process Selected")
    ButtonGadget(#Button_2,200,WindowHeight(#Window_0)-35,150,25,"Clear All Checkboxes")
    
    ; load some example data
    AddGadgetItem(#ListIcon_1,-1," Data 1")
    AddGadgetItem(#ListIcon_1,-1," Data 2")
    AddGadgetItem(#ListIcon_1,-1," Data 3")
    AddGadgetItem(#ListIcon_1,-1," Data 4")
    AddGadgetItem(#ListIcon_1,-1," Data 5")
    AddGadgetItem(#ListIcon_1,-1," Data 6")
    AddGadgetItem(#ListIcon_1,-1," Data 7")
    AddGadgetItem(#ListIcon_1,-1," Data 8")
    AddGadgetItem(#ListIcon_1,-1," Data 9")
    AddGadgetItem(#ListIcon_1,-1," Data 10")
    AddGadgetItem(#ListIcon_1,-1," Data 11")
    AddGadgetItem(#ListIcon_1,-1," Data 12")
    AddGadgetItem(#ListIcon_1,-1," Data 13")
    AddGadgetItem(#ListIcon_1,-1," Data 14")
    
  EndIf
  
EndProcedure

OpenWindow_Window_0()

Repeat
  
  Select WaitWindowEvent()
      
    Case #PB_Event_Gadget
      
      Select EventGadget ()
          
        Case #ListIcon_1
          Select EventType ()
            Case #PB_EventType_Change
              If SelectedPages()=1
                Tag=1
              EndIf
          EndSelect
          
        Case #Button_1
          
          SetGadgetState(#ListIcon_1, -1) ;workaround for last selected item (unselect all items in list) 
          
          Mess.s=""
          For a=0 To CountGadgetItems(#ListIcon_1)-1
            Selected=GetGadgetItemState(#ListIcon_1, a)
            If Selected=#PB_ListIcon_Checked
              If Mess=""
                Mess=Mess+GetGadgetItemText(#ListIcon_1,a,0)
              Else
                Mess=Mess+","+GetGadgetItemText(#ListIcon_1,a,0)
              EndIf
            EndIf
          Next
          MessageRequester("You selected the following",Mess)
          
          
        Case #Button_2
          For a=0 To CountGadgetItems(#ListIcon_1)-1
            SetGadgetItemState(#ListIcon_1,a,0)
          Next
          
          
      EndSelect
      
      
    Case #PB_Event_CloseWindow
      EventWindow = EventWindow()
      If EventWindow = #Window_0
        CloseWindow(#Window_0)
        Break
      EndIf
  EndSelect
  
  
  If SelectedPages()>1 And Tag=1 ; Multiselect checkboxes
    Tag=0
    For a=0 To CountGadgetItems(#ListIcon_1)-1
      Selected=GetGadgetItemState(#ListIcon_1, a)
      If Selected=#PB_ListIcon_Selected
        SetGadgetItemState(#ListIcon_1,a,#PB_ListIcon_Checked)
      EndIf
    Next
  EndIf
  
ForEver