How to make a Dialog as a function?

Hi there!
Let me state my dilema. I’m most sure that there must be working codes
that does what I want.
Usually, a function is just a “black box” that you feed “something”,
then it “does something to it”, and it throws out “something else”.
i.e. hometown = find_hometown(employee)

Well, I’d like to have a dialog where you can choose from a list, click
ok and the dialog would return an answer
i.e. hometown = mydlg.find_hometown(employee_array)

My logics dilema is that, find_hometown must do the following:

  • Instance the dialog
  • Fill a Wx::Choice with employee_array
  • show modal
  • Let the user choose and click OK
  • Do some SQL magic to retrieve the hometown of the selected item
  • Return the value
  • Destroy the dialog.
    Everything is ok with this procedure, except that:
    As in ruby, the last eval is what is returned, how can I self.distroy
    the dialog if the return value must be the LAST line of code in the
    function?

I’ve noticed that MessageDialog displays such effect, but, I’d really
like to know the logic behind it.

Thx everyone!


Carlos Troncoso Phillips

Hi

Carlos Troncoso Phillips wrote:

Well, I’d like to have a dialog where you can choose from a list, click
ok and the dialog would return an answer
i.e. hometown = mydlg.find_hometown(employee_array)

The class to start with is Wx::SingleChoiceDialog. Your message made me
look at this class for the first time and it needs a little developer
attention, but it should work fine for your purposes.

With dialogs the primary return value is whether or not the user chose
to proceed - ie did they click ‘OK’ or ‘Cancel’ (or shut the dialog, or
press )? In deciding what to do next, an application should test
this before it thinks about acting upon what value was chosen.

Conventionally, wxRuby dialogs signal this by returning an integer
constant from show_modal. Your code should test this value, then proceed
accordingly.

As in ruby, the last eval is what is returned, how can I self.distroy
the dialog if the return value must be the LAST line of code in the
function?

See the example code below as a starting point. It shows how, if you
want, you can provide a class method as a quick way to select something
from a list, or return ‘nil’ if the user cancelled the action.

If you create a dialog with a parent window, then show it temporarily,
you don’t need to call #destroy on it. User code rarely need to call
#destroy (see the docs); in most normal cases wxRuby handles window
deletion and clean up automatically.

alex
__
require ‘wx’

class EmployeeDialog < Wx::SingleChoiceDialog
EMPLOYEES = { ‘Fred’ => ‘London’,
‘Brenda’ => ‘Manchester’,
‘Billy’ => ‘Glasgow’ }

def self.find_hometown(parent)
dlg = new(parent, ‘Select employee’, ‘Choose one’, EMPLOYEES.keys)
if dlg.show_modal == Wx::ID_OK
return dlg.hometown
else
return nil
end
end

def hometown
EMPLOYEES[string_selection]
end
end

Wx::App.run do
frame = Wx::Frame.new nil, :title => ‘Employeees’
button = Wx::Button.new frame, :label => ‘Select employee’
evt_button(button) { puts EmployeeDialog.find_hometown(frame) }
frame.show
end