Reading groups of RadioButtons

Post your Gambas programming questions here.
Post Reply
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Reading groups of RadioButtons

Post by Got2BeFree »

Is there a more elegant (or proper) way of reading the value(s) of a group of RadioButtons to determine which one is True?

For instance... If I have a group of 4 RadioButtons (in a panel) and I want to set the selected button's value in a config file (or just to determine which button is "True"), is there a better way than doing this...
    If RadioButton1.Value = True Then hSetting["System/Header/Main"] = 0
    If RadioButton2.Value = True Then hSetting["System/Header/Main"] = 1
    If RadioButton3.Value = True Then hSetting["System/Header/Main"] = 2
    If RadioButton4.Value = True Then hSetting["System/Header/Main"] = 3
Or is that the only way?
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: Reading groups of RadioButtons

Post by jornmo »

How about defining the Action property on all RadioButtons, create a subroutine for that action that defines the value of a global variable that get's the value from the RadioButton's Tag property (using .Last). From there you save the setting using that variable.
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Reading groups of RadioButtons

Post by cogier »

If you change the name of the RadioButtons to: -
RadioButton0
RadioButton1
RadioButton2
RadioButton3

Add a group name to all the RadioButtons e.g AllRadioButtons then add the following code: -
Public Sub AllRadioButtons_Click()

Settings["System/Header/Main"] = Right(Last.name)
Settings.Save

End
This is a solution provided you only want to catch this when the value of the RadioButtons change.
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: Reading groups of RadioButtons

Post by Got2BeFree »

Thanks, guys! That jogs my memory a little (it's not so good anymore). IIRC, I used a similar way (using .Last) of doing it way back on Gambas 2.

My 3-year old appointment reminder app has had numerous RadioButtons added to the config section over the years and I'm starting to look at reducing the number of code lines. I may even look at switching to using ComboBoxes like I do in my inventory app. ComboBoxes has the added benefit of reducing GUI clutter.
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: Reading groups of RadioButtons

Post by jornmo »

Ahh, yes! Sorry :) I meant Group, not Action.
Post Reply