Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 6:43 pm
by TN6
Hi
I am a starter here and my first program I have a problem.
How can i check check-boxes state with a loop?
I am thinking like this.
k=1
For i=0 to 9
j= GetGadgetState(Checkbox_i)
if j=1
k=k+1
end if
next i
Thanks
TN
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 6:51 pm
by firace
At first glance, that looks good to me... at least if all you need is the number of ticked boxes.
What actual problem or error are you running into?
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 7:57 pm
by TN6
Hi
This is the exact code .
Case Button_0 ;Go
k=1
For i=0 To 9
j= GetGadgetState(Checkbox_i)
If j=1
k=k+1
EndIf
Next i
MessageRequester("Information",Str(k), 0)
this is the error message at j= GetGadgetState(Checkbox_i)
[20:51:32] [ERROR] copy-2.pb (Line: 103)
[20:51:32] [ERROR] The specified #Gadget is not initialised.
Thanks
TN
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 8:14 pm
by Demivec
For posting code in a message it is very helpful to use the code tags when entering the message. When you do your code is easier to separate from the rest of the message and looks like this:
Code: Select all
Case Button_0 ;Go
k=1
For i=0 To 9
j= GetGadgetState(Checkbox_i)
If j=1
k=k+1
EndIf
Next i
MessageRequester("Information",Str(k), 0)
[20:51:32] [ERROR] The specified #Gadget is not initialised.
You need to supply individual gadget #'s for use with GetGadgetState(). In the code you tried to create what looks like code that you thought would be interpreted while it is run to created the proper value (i.e. a combination of 'Checkbox_' + the value of i). This won't work, or at least not in this manner.
One option include using constants for the gadgets:
Code: Select all
;defined previously when the checkboxes are created
#Checkbox_0 = 0 ;checkboxes are declared with the gadget#'s #Checkbox_0 --> #Checkbox_0 + 9
;your code snippet with the modifications
Case Button_0 ;Go
k=1
For i=0 To 9
j= GetGadgetState(#Checkbox_0 + i)
If j=1
k=k+1
EndIf
Next i
MessageRequester("Information",Str(k), 0)
Other options would require a little more code to represent but include keeping track of the gadget#'s for the checkboxes (especially if they are created dynamically using #PB_Any) in either an array or list and then cycling through that using each of the gadget#'s individually to check their state.
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 8:16 pm
by mk-soft
That's not enough for a long time.
What is the window? Where are the checkboxes?
What are the gadgets numbered?
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 8:31 pm
by TN6
Hi
This is the program.
I copied the suggested lines in my code and i got the same error!!!!
Thanks
TN
;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;
Global Window_0
Global Checkbox_0, Checkbox_1, Checkbox_2, Checkbox_3, Checkbox_4, Checkbox_5, Checkbox_6, Checkbox_7, Checkbox_8, Checkbox_9, Option_0, Option_1, Frame3D_0, Button_0, Button_1, Button_3, Button_4, Button_5
Procedure OpenWindow_0(x = 0, y = 0, width = 620, height = 260)
Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Checkbox_0 = CheckBoxGadget(#PB_Any, 50, 80, 100, 25, " xyplorer")
Checkbox_1 = CheckBoxGadget(#PB_Any, 50, 110, 100, 25, " 7zip")
Checkbox_2 = CheckBoxGadget(#PB_Any, 50, 140, 100, 25, " FS viewer")
Checkbox_3 = CheckBoxGadget(#PB_Any, 50, 170, 100, 25, " FS capture")
Checkbox_4 = CheckBoxGadget(#PB_Any, 50, 200, 100, 25, " Filezilla")
Checkbox_5 = CheckBoxGadget(#PB_Any, 190, 80, 100, 25, " Firefox")
Checkbox_6 = CheckBoxGadget(#PB_Any, 190, 110, 100, 25, " Levenhuk")
Checkbox_7 = CheckBoxGadget(#PB_Any, 190, 140, 100, 25, " Skype")
Checkbox_8 = CheckBoxGadget(#PB_Any, 190, 170, 100, 25, " Purebasic")
Checkbox_9 = CheckBoxGadget(#PB_Any, 190, 200, 140, 25, " Norton removal")
#Checkbox_0 = 0 ;checkboxes are declared with the gadget#'s #Checkbox_0 --> #Checkbox_0 + 9
Option_0 = OptionGadget(#PB_Any, 380, 40, 110, 20, "C:\Users\TN\")
SetGadgetState(Option_0, 1)
Option_1 = OptionGadget(#PB_Any, 380, 70, 120, 25, "New location")
Frame3D_0 = FrameGadget(#PB_Any, 360, 20, 220, 90, "")
Button_0 = ButtonGadget(#PB_Any, 360, 130, 100, 25, "Go")
Button_1 = ButtonGadget(#PB_Any, 480, 130, 100, 25, "Cancel")
Button_4 = ButtonGadget(#PB_Any, 50, 40, 100, 25, "select all")
Button_5 = ButtonGadget(#PB_Any, 190, 40, 100, 25, "unselect all")
EndProcedure
Procedure Window_0_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case Button_1 ; Quit...
Event = #PB_Event_CloseWindow
Case Button_4 ; select all
SetGadgetState(Checkbox_0,1)
SetGadgetState(Checkbox_1,1)
SetGadgetState(Checkbox_2,1)
SetGadgetState(Checkbox_3,1)
SetGadgetState(Checkbox_4,1)
SetGadgetState(Checkbox_5,1)
SetGadgetState(Checkbox_6,1)
SetGadgetState(Checkbox_7,1)
SetGadgetState(Checkbox_8,1)
SetGadgetState(Checkbox_9,1)
Case Button_5 ; unselect all
SetGadgetState(Checkbox_0,0)
SetGadgetState(Checkbox_1,0)
SetGadgetState(Checkbox_2,0)
SetGadgetState(Checkbox_3,0)
SetGadgetState(Checkbox_4,0)
SetGadgetState(Checkbox_5,0)
SetGadgetState(Checkbox_6,0)
SetGadgetState(Checkbox_7,0)
SetGadgetState(Checkbox_8,0)
SetGadgetState(Checkbox_9,0)
Case Option_1 ;New location
InitialPath$ = "C:\Users\Tibor\" ; set initial path to display (could also be blank)
Path$ = PathRequester("Please choose your path", InitialPath$)
If Path$
MessageRequester("Information", "You have selected following path:"+Chr(10)+Path$, 0)
Else
MessageRequester("Information", "The requester was canceled.", 0)
EndIf
Case Button_0 ;Go
#Checkbox_0 = 0 ;checkboxes are declared with the gadget#'s #Checkbox_0 --> #Checkbox_0 + 9
k=1
For i=0 To 9
j= GetGadgetState(#Checkbox_0 + i)
If j=1
k=k+1
EndIf
Next i
MessageRequester("Information",Str(k), 0)
EndSelect
EndIf
Until Window_0_Events(event) = #False
End
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 8:56 pm
by Paul
When using #PB_Any you must keep track of the entries since #PB_Any creates gadget numbers randomly.
Quick example using #PB_Any...
Code: Select all
Global Window_Main
Global Gadget_Main_Ok
Global Dim CheckBox(10)
Procedure.i Window_Main()
Window_Main=OpenWindow(#PB_Any,190,136,296,282,"Work Form1",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
If Window_Main
CheckBox(1)=CheckBoxGadget(#PB_Any,40,20,80,15,"CheckBox2")
CheckBox(2)=CheckBoxGadget(#PB_Any,40,40,80,15,"CheckBox3")
CheckBox(3)=CheckBoxGadget(#PB_Any,40,60,80,15,"CheckBox4")
CheckBox(4)=CheckBoxGadget(#PB_Any,40,80,80,15,"CheckBox5")
CheckBox(5)=CheckBoxGadget(#PB_Any,40,100,80,15,"CheckBox6")
CheckBox(6)=CheckBoxGadget(#PB_Any,40,120,80,15,"CheckBox7")
CheckBox(7)=CheckBoxGadget(#PB_Any,40,140,80,15,"CheckBox8")
CheckBox(8)=CheckBoxGadget(#PB_Any,40,160,80,15,"CheckBox9")
CheckBox(9)=CheckBoxGadget(#PB_Any,40,180,80,15,"CheckBox10")
CheckBox(10)=CheckBoxGadget(#PB_Any,40,200,80,15,"CheckBox11")
Gadget_Main_Ok=ButtonGadget(#PB_Any,200,240,60,20,"Select All")
HideWindow(Window_Main,0)
ProcedureReturn WindowID(Window_Main)
EndIf
EndProcedure
;- Main Loop
If Window_Main()
Define quitMain=0
Repeat
EventID =WaitWindowEvent()
MenuID =EventMenu()
GadgetID =EventGadget()
WindowID =EventWindow()
Select EventID
Case #PB_Event_CloseWindow
If WindowID=Window_Main
quitMain=1
EndIf
Case #PB_Event_Gadget
Select GadgetID
Case Gadget_Main_Ok
For tmp=1 To 10
SetGadgetState(checkbox(tmp),1)
Next
EndSelect
EndSelect
Until quitMain
CloseWindow(Window_Main)
EndIf
End
Here is another example using Enumeration. This works because gadget numbers are created in sequence...
Code: Select all
;- Window Constants
Enumeration 1
#Window_Main
EndEnumeration
;- Gadget Constants
Enumeration 1
;Window_Main
#Gadget_Main_CheckBox2
#Gadget_Main_CheckBox3
#Gadget_Main_CheckBox4
#Gadget_Main_CheckBox5
#Gadget_Main_CheckBox6
#Gadget_Main_CheckBox7
#Gadget_Main_CheckBox8
#Gadget_Main_CheckBox9
#Gadget_Main_CheckBox10
#Gadget_Main_CheckBox11
#Gadget_Main_Ok
EndEnumeration
Procedure.i Window_Main()
If OpenWindow(#Window_Main,190,136,296,282,"Work Form1",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
CheckBoxGadget(#Gadget_Main_CheckBox2,40,20,80,15,"CheckBox2")
CheckBoxGadget(#Gadget_Main_CheckBox3,40,40,80,15,"CheckBox3")
CheckBoxGadget(#Gadget_Main_CheckBox4,40,60,80,15,"CheckBox4")
CheckBoxGadget(#Gadget_Main_CheckBox5,40,80,80,15,"CheckBox5")
CheckBoxGadget(#Gadget_Main_CheckBox6,40,100,80,15,"CheckBox6")
CheckBoxGadget(#Gadget_Main_CheckBox7,40,120,80,15,"CheckBox7")
CheckBoxGadget(#Gadget_Main_CheckBox8,40,140,80,15,"CheckBox8")
CheckBoxGadget(#Gadget_Main_CheckBox9,40,160,80,15,"CheckBox9")
CheckBoxGadget(#Gadget_Main_CheckBox10,40,180,80,15,"CheckBox10")
CheckBoxGadget(#Gadget_Main_CheckBox11,40,200,80,15,"CheckBox11")
ButtonGadget(#Gadget_Main_Ok,200,240,60,20,"Select All")
HideWindow(#Window_Main,0)
ProcedureReturn WindowID(#Window_Main)
EndIf
EndProcedure
;- Main Loop
If Window_Main()
Define quitMain=0
Repeat
EventID =WaitWindowEvent()
MenuID =EventMenu()
GadgetID =EventGadget()
WindowID =EventWindow()
Select EventID
Case #PB_Event_CloseWindow
If WindowID=#Window_Main
quitMain=1
EndIf
Case #PB_Event_Gadget
Select GadgetID
Case #Gadget_Main_Ok
For tmp=#Gadget_Main_CheckBox2 To #Gadget_Main_CheckBox11
SetGadgetState(tmp,1)
Next
EndSelect
EndSelect
Until quitMain
CloseWindow(#Window_Main)
EndIf
End
Your code does not work as expected because Checkbox_0 could =1234 and Checkbox_1 could = 3456
so Checkbox_0+i would be 1234+1
Re: Checking 10 check-boxes State with loop
Posted: Sun Jul 19, 2015 9:11 pm
by BasicallyPure
I would not recommend using the form designer as a beginner.
It's better to learn to write the code yourself then use the form designer for
larger projects.
Here is your code edited using the enumeration method as mentioned by Paul.
Code: Select all
Global Window_0
Global Option_0, Option_1, Frame3D_0, Button_0, Button_1, Button_3, Button_4, Button_5
Enumeration
#Checkbox_0
#Checkbox_1
#Checkbox_2
#Checkbox_3
#Checkbox_4
#Checkbox_5
#Checkbox_6
#Checkbox_7
#Checkbox_8
#Checkbox_9
EndEnumeration
Procedure OpenWindow_0(x = 0, y = 0, width = 620, height = 260)
Window_0 = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CheckBoxGadget(#Checkbox_0, 50, 80, 100, 25, " xyplorer")
CheckBoxGadget(#Checkbox_1, 50, 110, 100, 25, " 7zip")
CheckBoxGadget(#Checkbox_2, 50, 140, 100, 25, " FS viewer")
CheckBoxGadget(#Checkbox_3, 50, 170, 100, 25, " FS capture")
CheckBoxGadget(#Checkbox_4, 50, 200, 100, 25, " Filezilla")
CheckBoxGadget(#Checkbox_5, 190, 80, 100, 25, " Firefox")
CheckBoxGadget(#Checkbox_6, 190, 110, 100, 25, " Levenhuk")
CheckBoxGadget(#Checkbox_7, 190, 140, 100, 25, " Skype")
CheckBoxGadget(#Checkbox_8, 190, 170, 100, 25, " Purebasic")
CheckBoxGadget(#Checkbox_9, 190, 200, 140, 25, " Norton removal")
;#Checkbox_0 = 0 ;checkboxes are declared with the gadget#'s #Checkbox_0 --> #Checkbox_0 + 9
Option_0 = OptionGadget(#PB_Any, 380, 40, 110, 20, "C:\Users\TN\")
SetGadgetState(Option_0, 1)
Option_1 = OptionGadget(#PB_Any, 380, 70, 120, 25, "New location")
Frame3D_0 = FrameGadget(#PB_Any, 360, 20, 220, 90, "")
Button_0 = ButtonGadget(#PB_Any, 360, 130, 100, 25, "Go")
Button_1 = ButtonGadget(#PB_Any, 480, 130, 100, 25, "Cancel")
Button_4 = ButtonGadget(#PB_Any, 50, 40, 100, 25, "select all")
Button_5 = ButtonGadget(#PB_Any, 190, 40, 100, 25, "unselect all")
EndProcedure
Procedure Window_0_Events(event)
Select event
Case #PB_Event_CloseWindow
ProcedureReturn #False
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
ProcedureReturn #True
EndProcedure
OpenWindow_0()
Repeat
event = WaitWindowEvent()
If Event = #PB_Event_Gadget
Select EventGadget()
Case Button_1 ; Quit...
Event = #PB_Event_CloseWindow
Case Button_4 ; select all
SetGadgetState(#Checkbox_0,1)
SetGadgetState(#Checkbox_1,1)
SetGadgetState(#Checkbox_2,1)
SetGadgetState(#Checkbox_3,1)
SetGadgetState(#Checkbox_4,1)
SetGadgetState(#Checkbox_5,1)
SetGadgetState(#Checkbox_6,1)
SetGadgetState(#Checkbox_7,1)
SetGadgetState(#Checkbox_8,1)
SetGadgetState(#Checkbox_9,1)
Case Button_5 ; unselect all
SetGadgetState(#Checkbox_0,0)
SetGadgetState(#Checkbox_1,0)
SetGadgetState(#Checkbox_2,0)
SetGadgetState(#Checkbox_3,0)
SetGadgetState(#Checkbox_4,0)
SetGadgetState(#Checkbox_5,0)
SetGadgetState(#Checkbox_6,0)
SetGadgetState(#Checkbox_7,0)
SetGadgetState(#Checkbox_8,0)
SetGadgetState(#Checkbox_9,0)
Case Option_1 ;New location
InitialPath$ = "C:\Users\Tibor\" ; set initial path to display (could also be blank)
Path$ = PathRequester("Please choose your path", InitialPath$)
If Path$
MessageRequester("Information", "You have selected following path:"+Chr(10)+Path$, 0)
Else
MessageRequester("Information", "The requester was canceled.", 0)
EndIf
Case Button_0 ;Go
;#Checkbox_0 = 0 ;checkboxes are declared with the gadget#'s #Checkbox_0 --> #Checkbox_0 + 9
k=0
;For i=0 To 9
For i = #Checkbox_0 To #Checkbox_9
j= GetGadgetState(i)
If j=1
k=k+1
EndIf
Next i
MessageRequester("Information",Str(k), 0)
EndSelect
EndIf
Until Window_0_Events(event) = #False
End