Forum: wxRuby Wx::Dialog: run code when OK button selected

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
8e66f5ed440e8c12a0786ac2840a66c0?d=identicon&s=25 Josh K. (jeemang)
on 2010-06-11 00:59

I'm using wxRuby to build menus for a Sketchup plug-in I'm working on.
(Specifically, I'm actually using wxSU,,
which is basically wxRuby packaged up as a Sketchup plug-in).

I'm trying to create a custom dialog to gather information from the user
via text boxes (implemented with Wx::TextCtrl) and pull-down menus
(implemented with Wx::Choice). My problem is that I can't figure out how
to get the information entered via these means out of the Wx::Dialog
object they live in when the "OK" button is clicked. Here is the
relevant code:

## this is the WX code for an improved location dialog
class LocationDialog < Wx::Dialog

    def initialize

        ## set dialog characteristics
        title = "Location dialog"
        size =, 305)
        position = Wx::DEFAULT_POSITION
        style = Wx::SYSTEM_MENU
        name = "location dialog"

        ## create dialog
        super(, -1, title, position, size, style,

        ## fields and buttons
        ## city
        citySTPos =,10)
        citySTSize =,20)
        cityST =, -1, 'city', citySTPos,
citySTSize, Wx::ALIGN_RIGHT)

        cityTCPos =,10)
        cityTCSize =,20)
        @cityTC =, -1, '', cityTCPos, cityTCSize,

        ## timezone offset
        tzSTPos =,130)
        tzSTSize =,20)
        tzST =, -1, 'timezone offset', tzSTPos,

        tzCPos =,130)
        tzCSize =,20)
        tzChoices = (-12..12).to_a.collect{ |e| e.to_s }
        tzC =, -1, tzCPos, tzCSize, tzChoices)

        ## a bunch more fields here that I've omitted for brevity

        ## okay button
        okBPos =,255)
        okBSize =,20)
        okButton =, Wx::ID_OK, 'okay', okBPos,
okBSize, Wx::BU_BOTTOM)
        self.set_affirmative_id(okButton.get_id()) ## identifies button
as dialog "OK" button
      #evt_button(okButton.get_id()) {|e| on_okButton(e)} # attempt 1
        #evt_button(okButton.get_id()) {|e| puts "test" } # attempt 2

      ## cancel button
        canBPos =,255)
        canBSize =,20)
        canButton =, Wx::ID_CANCEL, 'cancel',
canBPos, canBSize, Wx::BU_BOTTOM)
        self.set_escape_id(canButton.get_id()) ## identifies button as
dialog "cancel" button

    end ## initilize

end ## LocationDialog

I tried using the button event handling block to point the button to
another method ("attempt 1" commented out above), but when I do it this
way, I can't get the LocationDialog.show_modal method to return anything
but a Fixnum 0 when "OK" is clicked (it should be returning either a
Fixnum 5100, or a Wx::ID_OK), which is problematic because I need this
return value for logic elsewhere in my code.

I also tried simply putting the code I need right in the block ("attempt
2", commented out above). When I do this the code in the block executes
as I would hope, but nothing else I need to happen happens -- ie, the
dialog doesn't close, and the LocationDialog.show_modal doesn't return
the correct value.

So: I'm wondering how I can accomplish this. Any suggestions would be
very much appreciated.

Mario Steele (Guest)
on 2010-06-11 23:27
(Received via mailing list)
Hello Josh,

Welcome to the wxRuby community, and thank you for using wxRuby with

While wxSU isn't a package we officially support, it's not much
then a Normal wxRuby application.  And your specific problem, does fall
the reign of wxRuby itself.  A common mis-conception about Dialogs, is
return values are anything of meaning, aside from the default values of
Wx::ID_OK, Wx::ID_CANCEL.  Wx::Dialog#show_modal will always return
values, to determine if the dialog was Cancelled, or okayed by the user.
 You should check for these values to ensure that your dialog was
by the user, before attempting to get any values from the dialog.

Now, with the establishment of how Wx::Dialog#show_modal returns values,
get your specific values out of the dialog, you have one of two ways in
which to gather the data from your custom Dialog.  Both methods are
in the way to extract the data, but one specific method is designed to
internal to your custom Dialog class, and the other is an external way.

The Internal way to get your data, is to create a method to retrieve the
values you need from your dialog, and return them as an array.

As such:

class LocationDialog < Wx::Dialog

  # After your initialization function add this:
  def get_values()
    values = []
    values << @cityTC.get_value
    values << @tzC.get_index
    return values

The other method, is similar to the above method, but instead of having
function built into your Dialog class, you make your control instances
attributes on your Dialog Class itself, to get the values out of them.

Like So:

class LocationDialog < Wx::Dialog
  attr_reader :cityTC, :tzC

  # Do your initialization stuff here

Now, in your main app, where you do the Dialog#show_modal, you can use
either method to get the data.

The first Method:

  def on_get_info()
    dlg =
    if dlg.show_modal == Wx::ID_OK
      data = dlg.get_values()
      # Parse your data here

The Second Method:

  def on_get_info()
    dlg =
    if dlg.show_modal == Wx::ID_OK
      city = dlg.cityTC.get_value
      tz = dlg.tzC.get_index
      # Parse your data here

My personal preference would be the first method, as all your data is
gathered at once.  But you can use any method you wish.  This is the
way to gather the data from a custom dialog that you have built.  If you
have any further questions, please feel free to ask on this list, as we
always happy to help out new comers.


8e66f5ed440e8c12a0786ac2840a66c0?d=identicon&s=25 Josh K. (jeemang)
on 2010-06-14 22:44
Hi Mario;

Thanks a lot for your response. I was able to solve my problem by using
"The First Method" you describe above. It's funny, I definitely had the
thought to create a method that would store the data collected from the
dialog's fields, but I couldn't figure out a way to call it between the
OK button being clicked and the dialog being destroyed. I'm a little
embarrassed I didn't think of using if/then -- thanks a lot for taking
the time to point it out to me!

Mario Steele (Guest)
on 2010-06-17 16:22
(Received via mailing list)
Not a problem Josh,

Don't feel to bad about it, everyone starts out not knowing a thing, and
they learn.  Sometimes from Documentation, sometimes from others.
you have a question, don't hesitate to ask, that's why we are here to
out the future developers that come along, wanting to use wxRuby in
shape or form, for something that they want to develop.

Have fun,

This topic is locked and can not be replied to.