On Sun, Aug 17, 2008 at 10:03 AM, Octavian Rasnita <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I need to create a modal dialog window with a few fields and after closing
> it, I need to return the values of those fields to the parent window.
>
> Please tell me which is the recommended way to do that. I've tried a few
> ways, but none of them looks clean.
>
> If I need to do what I said, do I need to use global variables like
>
> $main::var
> or
> $myapp->{var}
>
>
> If it is possible, I would like to be able to not use them, because the
> program would need too many and it will be hard to understand it after a
> time.
>
> I've tried something like:
>
> package MyApp::Main;
>
> use MyApp::Main::Dialog;
> use base 'Wx::Frame';
>
> sub new {
> my ($class, $parent) = @_
> my $self = $class->SUPER::new($parent, ...);
>
> $self->{dialog} = MyApp::Main::Dialog->new($self);
> $self->{dialog}->ShowModal();
>
> # Here I want to access the values from the fields from the closed dialog
> }
>
> I have also tried to store the code that creates the dialog in MyApp::Main,
> but the event handler which is executed when pressing the "Ok" button of the
> dialog gets the $self which is the instance of the dialog, and not the Main
> form, so I can't use it to store the variables I need.
>
> I'm pretty confused.
> Please tell me which do you think it is the cleanest way to do that.
>
> Thank you.
>
> Octavian
>
>

Hi Octavian,

I've always just used a hash reference to pass values back and forth.
There may be a more Wx-approved way of doing this, but references are
straightforward enough. I usually clean it up by moving a lot of the
code that deals with this out to other methods (usually a
fill_defaults() called from the constructor, and a validate_entries()
and save_entries() at the end), but you can organize it however you
want.

package MyApp::Main;
...
my %dialog_options = ( foo => 'bar' );
my $dialog = MyApp::Main::Dialog->new($self, \%dialog_options);
$dialog->ShowModal;
# $dialog_options{foo} now has inputted value

package MyApp::Main::Dialog;
sub new {
my ($class, $parent, $options_ref) = @_;
my $self = $class->SUPER::new(...);
$self->_create_widgets;
$self->{_options} = $options_ref;
$self->{FooWidget}->SetValue($self->{_options}->{foo});
}

sub ok_button_clicked {
my ($self) = @_;
$self->{_options}->{foo} = $self->{FooWidget}->GetValue;
$self->EndModal(wxID_OK);
}

-Brian

Reply via email to