Page 1 of 1
Independent groups of radio buttons? [SOLVED]
Posted: Tue Jul 04, 2017 11:58 am
by bmcs
I need to create 2 independent groups of radio buttons (OptionGadget) in the same window.
For example:
_______________________
|-- Group A -|-- Group B -|
|_____________________|
| o Option 1 | o Option 1 |
| o Option 2 | o Option 2 |
| o Option 3 | o Option 3 |
-------------------------------
Without some form grouping, selecting an option in one group removes the selection in the other group.
Any pointers how to do this in PB would be appreciated.
Dave
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 12:18 pm
by Dude
The manual says to create a different gadget to finish the current group. So, as an example:
Code: Select all
If OpenWindow(0, 0, 0, 240, 110, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OptionGadget(1, 30, 20, 60, 20, "Option 1")
OptionGadget(2, 30, 45, 60, 20, "Option 2")
OptionGadget(3, 30, 70, 60, 20, "Option 3")
TextGadget(9,0,0,0,0,"") ; End current group.
OptionGadget(4, 130, 20, 60, 20, "Option 4")
OptionGadget(5, 130, 45, 60, 20, "Option 5")
OptionGadget(6, 130, 70, 60, 20, "Option 6")
SetGadgetState(4, 1) ; Set second group as active one
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 12:40 pm
by RASHAD
Or
Code: Select all
If OpenWindow(0, 0, 0, 220, 200, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ContainerGadget(10,10,10,100,100,#PB_Container_Flat)
StringGadget(0,-1,-1,92,20," Group #1",#PB_String_ReadOnly)
OptionGadget(1, 15, 20, 60, 20, "Option 1")
OptionGadget(2, 15, 45, 60, 20, "Option 2")
OptionGadget(3, 15, 70, 60, 20, "Option 3")
CloseGadgetList()
ContainerGadget(20,101,10,100,100,#PB_Container_Flat)
StringGadget(4,-1,-1,102,20," Group #2",#PB_String_ReadOnly)
OptionGadget(5, 15, 20, 60, 20, "Option 4")
OptionGadget(6, 15, 45, 60, 20, "Option 5")
OptionGadget(7, 15, 70, 60, 20, "Option 6")
CloseGadgetList()
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 12:51 pm
by bmcs
Thanks Rashad,
The ContainerGadget is the facility I expected to find, but again overlooked it in the manual.
Obviously there is a lot more me to find on my PB voyage of discovery.
Regards
Dave
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 1:19 pm
by RASHAD
Thanks bmcs
If you are using PB v5.6 it will be more fun
Code: Select all
If OpenWindow(0, 0, 0, 220, 200, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(10,10,10,200,130,#PB_Canvas_Container)
StartDrawing(CanvasOutput(10))
Box(0,0,200,130,0)
Box(5,5,190,120,$FFFFFF)
Box(5,25,190,5,0)
Box(95,0,5,130,0)
DrawText(20,6,"Group A",0,$FFFFFF)
DrawText(115,6,"Group B",0,$FFFFFF)
StopDrawing()
OptionGadget(1, 15, 40, 60, 20, "Option 1")
OptionGadget(2, 15, 65, 60, 20, "Option 2")
OptionGadget(3, 15, 90, 60, 20, "Option 3")
TextGadget(4,0,0,0,0,"")
OptionGadget(5, 115, 40, 60, 20, "Option 4")
OptionGadget(6, 115, 65, 60, 20, "Option 5")
OptionGadget(7, 115, 90, 60, 20, "Option 6")
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 2:57 pm
by bmcs
Thanks again Rashid. Very nice. Lots of possibilities here.
What I like about this forum is you get real help, not just RTFM!
Dave
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 7:08 pm
by Shardik
An alternative to Dude's solution (which uses an invisible TextGadget to switch between option groups) was proposed by Ligatur
in the German forum: he uses a Windows API function to get the window style of the first gadget in the second group and sets the attribute #WS_Group. Because of using a Windows API function this solution only works on Windows:
Code: Select all
If OpenWindow(0, 0, 0, 240, 110, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OptionGadget(1, 30, 20, 60, 20, "Option 1")
OptionGadget(2, 30, 45, 60, 20, "Option 2")
OptionGadget(3, 30, 70, 60, 20, "Option 3")
OptionGadget(4, 130, 20, 60, 20, "Option 4")
SetWindowLongPtr_(GadgetID(4), #GWL_STYLE, GetWindowLongPtr_(GadgetID(4),
#GWL_STYLE) | #WS_GROUP)
OptionGadget(5, 130, 45, 60, 20, "Option 5")
OptionGadget(6, 130, 70, 60, 20, "Option 6")
SetGadgetState(4, 1) ; Set second group as active one
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 7:43 pm
by bmcs
Thanks Shardik. My target for this exercise is both Windows & Linux, but I will definitely keep a note of this for future reference.
Regards Dave
Re: Independent groups of radio buttons?
Posted: Tue Jul 04, 2017 10:50 pm
by Demivec
This example does away with the creation of a spurious gadget as a way of ending a group of radio buttons. It simply reselects the same gadget list.
Code: Select all
If OpenWindow(0, 0, 0, 240, 110, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OptionGadget(1, 30, 20, 60, 20, "Option 1")
OptionGadget(2, 30, 45, 60, 20, "Option 2")
OptionGadget(3, 30, 70, 60, 20, "Option 3")
UseGadgetList(WindowID(0)) ;reselect gadget list instead of creating an unneeded gadget
OptionGadget(4, 130, 20, 60, 20, "Option 4")
OptionGadget(5, 130, 45, 60, 20, "Option 5")
OptionGadget(6, 130, 70, 60, 20, "Option 6")
SetGadgetState(4, 1) ; Set second group as active one
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Wed Jul 05, 2017 3:17 am
by kenmo
I have used this to break groups of OptionGadgets:
Code: Select all
FreeGadget(TextGadget(#PB_Any, 0, 0, 0, 0, ""))
But I like Demivec's method better, no temporary gadget.
You can use this variation, so that you don't need to specify what WindowID you're working in:
Code: Select all
UseGadgetList(UseGadgetList(#Null))
Re: Independent groups of radio buttons?
Posted: Wed Jul 05, 2017 8:00 am
by Marc56us
The classical way is to use
FrameGadget()
Code: Select all
EnableExplicit
Define MainWin = OpenWindow(#PB_Any, 0, 0, 310, 250, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
Define Frame1 = FrameGadget(#PB_Any, 10, 10, 140, 230, "Group A")
Define Opt_A1 = OptionGadget(#PB_Any, 30, 50, 100, 20, "Option 1")
Define Opt_A2 = OptionGadget(#PB_Any, 30, 100, 100, 20, "Option 2")
Define Opt_A3 = OptionGadget(#PB_Any, 30, 150, 100, 20, "Option 3")
Define Frame2 = FrameGadget(#PB_Any, 160, 10, 140, 230, "Group B")
Define Opt_B1 = OptionGadget(#PB_Any, 190, 50, 100, 20, "Option 1")
Define Opt_B2 = OptionGadget(#PB_Any, 190, 100, 100, 20, "Option 2")
Define Opt_B3 = OptionGadget(#PB_Any, 190, 150, 100, 20, "Option 3")
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Good practices:
- Alway uses EnableExplicit
It's binding, but it avoids typo errors (which are very long to debug
)
- Do not use numbers (directly) as gadget IDs, even for short code
Uses Enumeration or OS object ID with #PB_Any (Like sample code above)

Re: Independent groups of radio buttons?
Posted: Wed Jul 05, 2017 9:51 am
by bmcs
Thanks Marc56us,
Your example made it abundantly clear where I was going wrong in my original attempt.
I always try to use Enumeration, but in some instances it makes coding a bit more cumbersome & clunky. For example accessing gadget IDs in a For loop.
Noted your tip about EnableExplicit to help with debugging, which I seem to spend way too much time doing.
Again, thanks to everyone who has helped me with this, you guys are terrific.
Regards Dave
Re: Independent groups of radio buttons?
Posted: Wed Jul 05, 2017 3:20 pm
by blueb
Very nice Rashad and kenmo,
Normally PureBasic does not allow a user to adjust the OptionGadget background color,
without an WinAPI fix such as: CreateSolidBrush_
Trouble is ... it becomes Windows Only.
The thing I have yet to figure out is why your method allows the OptionGadget backgound to work without having to use the above fix.
see example provided...
Code: Select all
;==================================================================
;
; Author: Rashad
; Date: July 4, 2017
; Explain:
;
; Typical Usage: OptionGadget Sample using a Canvas Gadget (and colors)
;==================================================================
If OpenWindow(0, 0, 0, 220, 160, "OptionGadget Sample", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(10,10,10,200,130,#PB_Canvas_Container)
StartDrawing(CanvasOutput(10))
Box(0,0,200,130,$000000) ; Large background box, black in color
Box(5,5,190,120,$D7EBFA) ; Lighter box inside the above box to form thick black lines
Box(5,25,190,5,$000000) ; Forms black line under Column Headers (Group A, B)
Box(95,0,5,130,$000000) ; Center black divider line
DrawText(20,6,"Group A",$007FFF,$D7EBFA)
DrawText(115,6,"Group B",$007FFF,$D7EBFA)
StopDrawing()
OptionGadget(1, 15, 40, 60, 20, "Option 1")
OptionGadget(2, 15, 65, 60, 20, "Option 2")
OptionGadget(3, 15, 90, 60, 20, "Option 3")
UseGadgetList(UseGadgetList(#Null)) ; Create a 'group' for the OptionGadgets above. (kenmo)
OptionGadget(5, 115, 40, 60, 20, "Option 4")
OptionGadget(6, 115, 65, 60, 20, "Option 5")
OptionGadget(7, 115, 90, 60, 20, "Option 6")
; Form background color
SetWindowColor(0,$1D66CD)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Independent groups of radio buttons?
Posted: Wed Jul 05, 2017 8:06 pm
by Shardik
Demivec's solution with UseGadgetList(WindowID(0)) or kenmo's hint on using UseGadgetList(#Null) or UseGadgetList(0) is working nice on Windows but unfortunately doesn't work on MacOS where a change in one column also clears all option gadgets in the other column...

Re: Independent groups of radio buttons?
Posted: Thu Jul 06, 2017 5:40 pm
by mk-soft
Small trick...
Code: Select all
;==================================================================
;
; Author: Rashad
; Author2: mk-soft - OptionBar()
; Date: July 4, 2017
; Explain:
;
; Typical Usage: OptionGadget Sample using a Canvas Gadget (and colors)
;==================================================================
Macro OptionBar()
FreeGadget(FrameGadget(#PB_Any,0,0,0,0,""))
EndMacro
If OpenWindow(0, 0, 0, 220, 160, "OptionGadget Sample", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(10,10,10,200,130,#PB_Canvas_Container)
StartDrawing(CanvasOutput(10))
Box(0,0,200,130,$000000) ; Large background box, black in color
Box(5,5,190,120,$D7EBFA) ; Lighter box inside the above box to form thick black lines
Box(5,25,190,5,$000000) ; Forms black line under Column Headers (Group A, B)
Box(95,0,5,130,$000000) ; Center black divider line
DrawText(20,6,"Group A",$007FFF,$D7EBFA)
DrawText(115,6,"Group B",$007FFF,$D7EBFA)
StopDrawing()
OptionGadget(1, 15, 40, 60, 20, "Option 1")
OptionGadget(2, 15, 65, 60, 20, "Option 2")
OptionGadget(3, 15, 90, 60, 20, "Option 3")
OptionBar()
OptionGadget(5, 115, 40, 60, 20, "Option 4")
OptionGadget(6, 115, 65, 60, 20, "Option 5")
OptionGadget(7, 115, 90, 60, 20, "Option 6")
; Form background color
SetWindowColor(0,$1D66CD)
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf