Independent groups of radio buttons? [SOLVED]

Just starting out? Need help? Post your questions and find answers here.
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Independent groups of radio buttons? [SOLVED]

Post 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
Last edited by bmcs on Mon Jul 10, 2017 9:35 am, edited 1 time in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Independent groups of radio buttons?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Independent groups of radio buttons?

Post 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
Egypt my love
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Independent groups of radio buttons?

Post 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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Independent groups of radio buttons?

Post 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
Egypt my love
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Independent groups of radio buttons?

Post 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
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Independent groups of radio buttons?

Post 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
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Independent groups of radio buttons?

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Independent groups of radio buttons?

Post 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
User avatar
kenmo
Addict
Addict
Posts: 2033
Joined: Tue Dec 23, 2003 3:54 am

Re: Independent groups of radio buttons?

Post 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))
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Independent groups of radio buttons?

Post 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
:idea: 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)
:wink:
bmcs
User
User
Posts: 21
Joined: Sat Apr 01, 2017 12:47 pm

Re: Independent groups of radio buttons?

Post 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
User avatar
blueb
Addict
Addict
Posts: 1111
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Independent groups of radio buttons?

Post 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
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Independent groups of radio buttons?

Post 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... :?
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Independent groups of radio buttons?

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply