# wxRuby\_RadioBox\_choices

**URL:** <https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344>\
**Category:** wxRuby\
**Created:** [November 22, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344 "2009-11-22T22:58:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 22, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344/1 "2009-11-22T22:58:30Z")

</div>

Platform:  
Ubuntu 9.04 (up to date), ruby 1.8.7 (2008-08-11 patchlevel 72)  
[i486-linux],  
wxruby-2.0.1-x86-linux

I am trying to use wxFormBuilder, with the output being an XRC file, to  
produce a simple application that I already have working from a single  
file, non-XRC approach. To clarify my descriptions, a simple applicaton  
uses two Ruby files: the “Gui” file produced by XRCise from the XRC  
file, and the “App” file which establishes how the application operates.  
The “App” file ‘requires’ the “Gui” file.

I have a simple XRC-based example working correctly. However, one thing  
that I’d like to do generates an error.

In wxFormBuilder, I have a RadioBox with four buttons labeled: “A” “B”  
“C” “D”.

When I run the final program, everything works. Then, I try to change  
the button labels in the “App” file by the expression:  
@radiobox1.choices = [“w”, “x”, “y”, “z”] which produces an error  
stating that ‘choices’ in not a defined method.

Just as a test, the following worked fine: @radiobox1.label = “New  
label for Box”

I used :choices in the non-XRC version of this program that was  
contained in one file.

I cannot find any reference to “choices” within Wx::RadioBox, such as by  
doing the following:  
irb(main):027:0\> RadioBox.private\_instance\_methods.sort and several  
variations of this approach.

My questions are:

1. Why can’t I find anything within Wx::RadioBox that references  
:choices?

2. Is there a way to add a new method to RadioBox from within the “App”  
program when using the XRC approach?

Thanks

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 12:45pm UTC](https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344/2 "2009-11-23T12:45:11Z")

</div>

On Sun, Nov 22, 2009 at 4:58 PM, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The “App” file ‘requires’ the “Gui” file.  
> stating that ‘choices’ in not a defined method.  
> variations of this approach.
> 
> My questions are:
> 
> 1. Why can’t I find anything within Wx::RadioBox that references  
> :choices?

To my knowledge, there is no :choices method on Wx::RadioBox at all. So  
why  
it works in your self-coded one, and not in XRC one, I can’t explain,  
unless  
you created the choices method yourself.

> 1. Is there a way to add a new method to RadioBox from within the “App”  
> program when using the XRC approach?

Yes, there is a way to add new methods to a XRCized created class. You  
need  
to require the generated file first, before you append methods to the  
class,  
simply for the fact that if you create a class, before it’s defined, you  
can’t subclass it, and the subclassing is already done in the XRCized  
generated output.

The Correct Way:

require ‘my\_xrc\_generated\_file’

class MyXrcWindow  
def new\_method  
puts “Hello World from New Method!”  
end  
end

Keep in mind, that with the current version of XRCise, that if you need  
to  
define anything to be executed, when the class is created, use the  
method  
on\_init.

EG: If you do:

require ‘my\_xrc\_generated\_file’

class MyXrcWindow  
def on\_init  
puts “This will be fired when an instance is created.”  
@@instance\_number |= 0  
@@instance\_number += 1  
end  
end

hth,

Mario

Thanks

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 7:29pm UTC](https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344/3 "2009-11-23T19:29:21Z")

</div>

Mario S. wrote:

> On Sun, Nov 22, 2009 at 4:58 PM, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > The “App” file ‘requires’ the “Gui” file.  
> > stating that ‘choices’ in not a defined method.  
> > variations of this approach.
> > 
> > My questions are:
> > 
> > 1. Why can’t I find anything within Wx::RadioBox that references  
> > :choices?
> 
> To my knowledge, there is no :choices method on Wx::RadioBox at all. So  
> why  
> it works in your self-coded one, and not in XRC one, I can’t explain,  
> unless  
> you created the choices method yourself.
> 
> > 1. Is there a way to add a new method to RadioBox from within the “App”  
> > program when using the XRC approach?
> 
> Yes, there is a way to add new methods to a XRCized created class. You  
> need  
> to require the generated file first, before you append methods to the  
> class,  
> simply for the fact that if you create a class, before it’s defined, you  
> can’t subclass it, and the subclassing is already done in the XRCized  
> generated output.
> 
> The Correct Way:
> 
> require ‘my\_xrc\_generated\_file’
> 
> class MyXrcWindow  
> def new\_method  
> puts “Hello World from New Method!”  
> end  
> end
> 
> Keep in mind, that with the current version of XRCise, that if you need  
> to  
> define anything to be executed, when the class is created, use the  
> method  
> on\_init.
> 
> EG: If you do:
> 
> require ‘my\_xrc\_generated\_file’
> 
> class MyXrcWindow  
> def on\_init  
> puts “This will be fired when an instance is created.”  
> @@instance\_number |= 0  
> @@instance\_number += 1  
> end  
> end
> 
> hth,
> 
> Mario
> 
> Thanks

Thanks for your help Mario!

Your examples helped me get my program working, and helped my  
understanding.

As for there being no :choices within wx::RadioBox, there are two places  
that show it. The first is in the official wxRuby documentation, listed  
under wx::RadioBox, as follows:

RadioBox.new(Window parent, Integer id, String label,  
Point point = DEFAULT\_POSITION,  
Size size = DEFAULT\_SIZE,  
Array choices =,  
Integer major\_dimension = 0,  
Integer style = RA\_SPECIFY\_COLS,  
Validator validator = DEFAULT\_VALIDATOR,  
String name = “radioBox”)

Constructor, creating and showing a radiobox.  
Parameters

```
* parent Parent window. Must not be nil.
* id Window identifier. A value of -1 indicates a default value.
* label Label for the static box surrounding the radio buttons.
* pos Window position. If the position (-1, -1) is specified then a 

```

## default position is chosen. \* size Window size. If the default size (-1, -1) is specified then a default size is chosen. \* choices An array of String choices with which to initialize the radiobox. \* major\_dimension Specifies the maximum number of rows (if style contains RA\_SPECIFY\_ROWS) or columns (if style contains RA\_SPECIFY\_COLS) for a two-dimensional radiobox. \* style Window style. See RadioBox. \* validator Window validator. \* name Window name.

The second place is in the tutorial below, which was the non-XRC program  
that I was referring to in my original post. See the following excerpt  
from that tutorial:

WxRubyWiki\_\_Buttons tutorial contains the following:  
Working with radio buttons  
#March 29, 2009  
#wxruby 2.0.0  
#Ruby 1.8.6  
#mac osx 10.4.11  
#7stud  
require “rubygems”  
require “wx”  
class MyFrame \< Wx::Frame  
def initialize  
super(nil, #Parent  
:title =\> “RadioBox Example”, #Displays on top of window  
:pos =\> [150, 25], #or Wx::DEFAULT\_POSITION  
:size =\> [300, 200] #or Wx::DEFAULT\_SIZE  
#For the position and size arguments, you don’t need to specify  
Point and Size  
#objects anymore. See: wxRuby Overview on the doc page  
wxruby\_intro.html  
)  
panel = Wx::Panel.new(self) #Parent = self = this Frame  
drink\_choices = [“coffee”, “tea”, “juice”, “milk”] #labels for  
radio buttons  
radios = Wx::RadioBox.new(  
panel, #Parent  
:label =\> “Drinks”, #Label for box surrounding radio  
buttons  
:pos =\> [20, 5],  
:size =\> Wx::DEFAULT\_SIZE,  
:choices =\> drink\_choices, #The labels for the radio buttons  
:major\_dimension =\> 1, #Max number of cols(or rows)–try  
changing it to 2  
:style =\> Wx::RA\_SPECIFY\_COLS #:major\_dimension value applies to  
columns  
#The :major\_dimension and :style in combination determine the  
layout of the  
#RadioBox. In this case, the maximum number of columns is 1,  
which limits  
#the number of radio buttons to one per line. Because there are  
4 radio buttons,  
#that means there will be 4 lines, with one radio button per  
line.  
)  
evt\_radiobox(radios.get\_id()) {|cmd\_event|  
on\_change\_radio(cmd\_event)}  
#When there is an event of type evt\_radiobox on the widget whose id  
is radios.get\_id(),  
#the block is called and an “event object” is passed to the block.  
The block calls  
#the method on\_change\_radio(), defined below, relaying the event  
object to the  
#method. The event object contains useful information about the  
radio button  
#that was selected.  
@text\_widget = Wx::StaticText.new(  
panel, #Parent  
:label =\> “coffee”,  
:pos =\> [150, 25],  
:size =\> Wx::DEFAULT\_SIZE  
#Store a widget in an instance variable if other  
#methods, like the one below, need access to it.  
)  
show #equivalent to self.show, makes the frame visible  
end  
def on\_change\_radio(cmd\_event)  
selected\_drink = cmd\_event.string #Selected radio’s label  
#Instead of calling methods like cmd\_event.get\_string() or  
cmd\_event.set\_string() you  
#can now call accessor methods with the same name as the property  
you are trying to  
#get or set. See: wxRuby Overview on the doc page wxruby\_intro.html  
@text\_widget.label = selected\_drink  
end  
end

class MinimalApp \< Wx::App  
def on\_init  
MyFrame.new  
end  
end

## MinimalApp.new.main\_loop #main\_loop() tells wxruby to start reacting #to events on your App, like radio button selections, #button clicks, etc.

So it seems that those ‘choices’ are arguments without associated  
methods. I was unable to get ‘choices’ listed as an argument. I only  
found ‘choices’ listed as an argument in an error message, where I had  
changed ‘choices’ to another word.

By the way, what are your examples supposed to do?  
@@instance\_number |= 0  
@@instance\_number += 1  
It seems to me that the first one creates a variable with a value of  
true.  
Executing the second one then generates an error, since it can’t  
increment true.

Gary

---

<div class="post-metadata">

**Author:** ![vanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac8455/32.png) [@vanik](https://www.ruby-forum.com/u/vanik)\
**Post date:** [November 23, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/wxruby-radiobox-choices/179344/4 "2009-11-23T21:07:23Z")

</div>

On Mon, Nov 23, 2009 at 1:29 PM, Gary H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > 1. Why can’t I find anything within Wx::RadioBox that references
> > > 2. Is there a way to add a new method to RadioBox from within the “App”
> > 
> > Keep in mind, that with the current version of XRCise, that if you need  
> > def on\_init  
> > Thanks
> 
> Thanks for your help Mario!
> 
> Your examples helped me get my program working, and helped my  
> understanding.

Not a problem.

> ```
> Validator validator = DEFAULT_VALIDATOR,
> 
> ```
> 
> - size Window size. If the default size (-1, -1) is specified then a
> 
> #7stud  
> #objects anymore. See: wxRuby Overview on the doc page  
> :size =\> Wx::DEFAULT\_SIZE,  
> 4 radio buttons,  
> object to the  
> )  
> end  
> selections,  
> #button clicks, etc

These are examples of Named parameters, as the Official WxWidgets API,  
the  
construction of a wxRadioBox allows you to pass a wxList of Labels to  
it.  
There’s no official method in which choices are mapped, though  
technically,  
all you would need to do, is write the method choices, in a way that  
enumerates the children of the wxRadioBox for retrieval, and a method in  
which deletes the children, and adds new ones to it.

* * *

> true.  
> Executing the second one then generates an error, since it can’t  
> increment true.

My apologies, I wrote that after I got home from work, when I was tired,  
and  
naturally, typo’ed the first line. The first line is suppose to read:  
@@instance\_number ||= 0, which means, unless there’s already a value  
stored  
in @@instance\_number, assign 0 to it. The next line, @@instance\_number  
+=  
1, is suppose to increment the number count of how many controls have  
been  
created.

In other words, if you have a dynamic control set being setup, and you  
want  
to monitor how many of the controls are created in a single run of the  
Application, you can use that method to see. In any case, hope these  
things  
help you out further in your development. Also note, that there is one  
method that I didn’t mention, as it’s not a method I personally like to  
use,  
but I wouldn’t be a good developer if I didn’t point it out.

While the on\_init method is something that I added to xrcise, as it’s  
something that I personally use, and my preferred way of access, and  
limiting the number of redundant classes and subclasses around in the  
name  
space of the application, it may be dropped in future versions of  
xrcise,  
though I prefer it would remain in the code, as it doesn’t effect  
anything  
with the generated code.

The other method in which to add methods to the generated class, is to  
simply create a new class definition, and subclass it to your XRC  
Generated  
class.

An Example:

require ‘my\_xrc\_class’

class MyRubyClass \< MyXrcClass  
def hello\_world  
self.set\_label(“Hello World!”)  
end  
end

Going this route, won’t hurt anything, and I believe it is of personal  
preference which method to use. Personally, I think the on\_init method  
is  
in tune with the wxRuby framework, as it is used in Wx::App. But keep  
in  
mind, that the on\_init method that is setup to execute from your XRCised  
code, is only available in that generated code, it’s not native to any  
specific Widget control implemented in wxRuby.

> ## Gary
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

hth,

Mario
