radio button's

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

radio button's

Frank Sonnemans-3
I am having some trouble getting radio buttons to work. Basically I have
a instance variable in my model which can take three possible values:
#red, #green or #blue. I would like to use radio buttons to change the
value of the instance var, but I don't understand how to do this since
the radio button expects to be coupled to a boolean.

Best Regards,


Frank


Reply | Threaded
Open this post in threaded view
|

Re: radio button's

Louis Sumberg-2
Take a look at Ian's RadioButtonGroup Goodie and the example he provides.
That might do it for you.

"Frank" <[hidden email]> wrote in message
news:[hidden email]...

> I am having some trouble getting radio buttons to work. Basically I have
> a instance variable in my model which can take three possible values:
> #red, #green or #blue. I would like to use radio buttons to change the
> value of the instance var, but I don't understand how to do this since
> the radio button expects to be coupled to a boolean.
>
> Best Regards,
>
>
> Frank
>


Reply | Threaded
Open this post in threaded view
|

Re: radio button's

Ian Bartholomew
In reply to this post by Frank Sonnemans-3
Frank,

> I am having some trouble getting radio buttons to work. Basically I have
> a instance variable in my model which can take three possible values:
> #red, #green or #blue. I would like to use radio buttons to change the
> value of the instance var, but I don't understand how to do this since
> the radio button expects to be coupled to a boolean.

It's a slightly roundabout procedure but the easiest way is to let the
buttons look after themselves (with a little help from Windows) and just
hook onto the #valueChanged event for all of the RadioButtoms you add. When
any of the buttons trigger the event, to say it has changed, you scan
through them all and locate the one that is selected. Roughly ...

createComponents
...
    redRB := self add: BooleanPresenter new name: 'red'.
    blueRB := self add: BooleanPresenter new name: 'blue'.
    greenRB := self add: BooleanPresenter new name: 'green.

createSchematicWiring
...
    (Array with: redRB with: blueRB with: greenRB) do: [:each |
        each when: #valueChanged send: #onRBValueChanged to: self]

onRBValueChanged
    | newSelection |
    (Array with: redRB with: blueRB with: greenRB) do: [:each |
        each value ifTrue: [newSelection := each name]].
    newSelection = currentRBSelection
        ifFalse: [
            currentRBSelection := newSelection.
            myModel colour: currentRBSelection].

The last bit is needed as both the button being cleared and the button being
selected trigger the initial #valueChanged event and you probably only want
to see it once. You could obviously get the same effect by doing something
similar in your model and losing the #currentRBSelection instVar.

Just in case you weren't aware - you must remember that all the RadioButtons
that you want to exhibit a group behaviour (when one is selected all the
others are unselected) must be placed within a single Dolphin ContainerView.

-~-~-~-~-

A simpler alternative, but which I would only recommend for a very small
number of RadioButtons, it to do the above but give each button it's own
event/method in #createSchematicWiring. You then end up with lots of methods
like

onRedRBValueChanged
    redRB value ifTrue: [myModel colour: #red]

-~-~-~-~-

Both the above can get a bit messy if you have lots of buttons or multiple
groups so I created a goodie (cue groans from the back of the room) which is
available on my web site and which helps with the RadioButton management. It
provides a new Container, RadioButtonGroup, which looks after all the
RadioButtons placed on it and provides a single event, also passing the name
of the selected button, to let you know when the selection has changed.

Regards
    Ian

http://www.iandb.org.uk


Reply | Threaded
Open this post in threaded view
|

Re: radio button's

Frank Sonnemans-3
Ian,

Thanks for taking the time to write such an extensive reply.

Frank

Ian Bartholomew wrote:

> Frank,
>
>
>>I am having some trouble getting radio buttons to work. Basically I have
>>a instance variable in my model which can take three possible values:
>>#red, #green or #blue. I would like to use radio buttons to change the
>>value of the instance var, but I don't understand how to do this since
>>the radio button expects to be coupled to a boolean.
>>
>
> It's a slightly roundabout procedure but the easiest way is to let the
> buttons look after themselves (with a little help from Windows) and just
> hook onto the #valueChanged event for all of the RadioButtoms you add. When
> any of the buttons trigger the event, to say it has changed, you scan
> through them all and locate the one that is selected. Roughly ...
>
> createComponents
> ...
>     redRB := self add: BooleanPresenter new name: 'red'.
>     blueRB := self add: BooleanPresenter new name: 'blue'.
>     greenRB := self add: BooleanPresenter new name: 'green.
>
> createSchematicWiring
> ...
>     (Array with: redRB with: blueRB with: greenRB) do: [:each |
>         each when: #valueChanged send: #onRBValueChanged to: self]
>
> onRBValueChanged
>     | newSelection |
>     (Array with: redRB with: blueRB with: greenRB) do: [:each |
>         each value ifTrue: [newSelection := each name]].
>     newSelection = currentRBSelection
>         ifFalse: [
>             currentRBSelection := newSelection.
>             myModel colour: currentRBSelection].
>
> The last bit is needed as both the button being cleared and the button being
> selected trigger the initial #valueChanged event and you probably only want
> to see it once. You could obviously get the same effect by doing something
> similar in your model and losing the #currentRBSelection instVar.
>
> Just in case you weren't aware - you must remember that all the RadioButtons
> that you want to exhibit a group behaviour (when one is selected all the
> others are unselected) must be placed within a single Dolphin ContainerView.
>
> -~-~-~-~-
>
> A simpler alternative, but which I would only recommend for a very small
> number of RadioButtons, it to do the above but give each button it's own
> event/method in #createSchematicWiring. You then end up with lots of methods
> like
>
> onRedRBValueChanged
>     redRB value ifTrue: [myModel colour: #red]
>
> -~-~-~-~-
>
> Both the above can get a bit messy if you have lots of buttons or multiple
> groups so I created a goodie (cue groans from the back of the room) which is
> available on my web site and which helps with the RadioButton management. It
> provides a new Container, RadioButtonGroup, which looks after all the
> RadioButtons placed on it and provides a single event, also passing the name
> of the selected button, to let you know when the selection has changed.
>
> Regards
>     Ian
>
> http://www.iandb.org.uk
>
>
>