Page 1 of 1

[Resolved] radio buttons - question

Posted: Thu May 29, 2025 4:37 pm
by SPH
Hi,
I have a list of radio buttons.
So far, so good.
But how do I know which one is checked?
I can't query each button number, can I?

Thanks!

Code: Select all

For i= 1 To 20
  OptionGadget(i, i*92-45, top+28, 60, 24, Str(i+2)+"x"+Str(i+2))
Next
  

Re: radio buttons - question

Posted: Thu May 29, 2025 4:43 pm
by Axolotl
add something like this to your event loop:

Code: Select all

;.......
        Case #PB_Event_Gadget 
          Select EventGadget() 
            Case 1 To 20  ; all OptionGadgets 
              Debug " => Option  " + EventGadget() 
          EndSelect 
;.......

Re: radio buttons - question

Posted: Thu May 29, 2025 4:45 pm
by Kiffi

Code: Select all

Procedure OptionGadgetEvent()
  
  Debug EventGadget()
  Debug GetGadgetText(EventGadget())
  
EndProcedure

OpenWindow(0, 0, 0, 1200, 200, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

For i= 1 To 20
  OptionGadget(i, i*92-45, top+28, 60, 24, Str(i+2)+"x"+Str(i+2))
  BindGadgetEvent(i, @OptionGadgetEvent())
Next

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow

Re: radio buttons - question

Posted: Thu May 29, 2025 4:54 pm
by SPH
Thank you guys for your reactivity.

I look that... :idea:

Re: radio buttons - question n°2

Posted: Thu May 29, 2025 5:36 pm
by SPH
I've refined my code to show you the situation I'm in.
Revisit my code word for word and correct the "debug"

Thanks

Code: Select all

If OpenWindow(0, 0, 0, 170, 160, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    OptionGadget(0, 30, 20, 90, 20, "Option 1")
    OptionGadget(1, 30, 45, 90, 20, "Option 2")
    OptionGadget(2, 30, 70, 90, 20, "Option 3")
    
    SetGadgetState(1, 1)   ; sélectionne la deuxième option
    
    ButtonGadget(10, 30,  100, 100, 40, "Save")

  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget
      
      Select EventGadget()
        Case 10 ; save pref
        Debug "the radio button is : "+who
      EndSelect
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow
  
EndIf

End


Re: radio buttons - question

Posted: Thu May 29, 2025 9:03 pm
by SPH
Reponse :

Code: Select all

If OpenWindow(0, 0, 0, 170, 160, "OptionGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    OptionGadget(0, 30, 20, 90, 20, "Option 1")
    OptionGadget(1, 30, 45, 90, 20, "Option 2")
    OptionGadget(2, 30, 70, 90, 20, "Option 3")
    
    SetGadgetState(1, 1)   ; sélectionne la deuxième option
    
    ButtonGadget(10, 30,  100, 100, 40, "Save")

  Repeat
    Event = WaitWindowEvent()
    
    If Event = #PB_Event_Gadget
      
      Select EventGadget()
        Case 10 ; save pref
          For i=0 To 2
            Debug "the radio button "+Str(i+1)+"is : "+GetGadgetState(i)
          Next
          
      EndSelect
      
    EndIf
    
  Until Event = #PB_Event_CloseWindow
  
EndIf

End


Re: [Resolved] radio buttons - question

Posted: Fri May 30, 2025 11:20 am
by AZJIO

Code: Select all

EnableExplicit

Define i, eg

If OpenWindow(0, 0, 0, 200, 610, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
	For i= 1 To 20
		ButtonGadget(i, 120, i * 28, 26, 26, Chr(i + 64))
	Next
	For i= 1 To 20
		OptionGadget(i + 20, 10, i * 28, 60, 24, Str(i))
	Next
	;- Loop
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_Gadget
				eg = EventGadget()
				Select eg
					Case 1 To 20
						Debug "Button" + Chr(eg + 64)
						SetGadgetText(eg, Chr(eg + 64 + 32))
					Case 21 To 40
						Debug "Option" + Str(eg - 20)
						SetGadgetText(eg, "click")
				EndSelect
			Case #PB_Event_CloseWindow
				CloseWindow(0)
				End
		EndSelect
	ForEver
EndIf

Re: [Resolved] radio buttons - question

Posted: Fri May 30, 2025 12:33 pm
by Axolotl
With these examples I would always strongly recommend the use of constants/enumerations. (just my opinion)

Re: [Resolved] radio buttons - question

Posted: Fri May 30, 2025 11:39 pm
by jacdelad
...depends on whether you know how many gadgets to create beforehand. Otherwise an array would be suitable too.