views:

41

answers:

1

I'm trying to add an event-handler to a subclass of Wx::StaticBoxSizer, but I'm getting the following error:

Can't locate object method "Connect" via package "Wx::StaticBoxSizer" at C:/strawberry/perl/site/lib/Wx/Event.pm line 38.

Does that mean that Wx::StaticBoxSizer can't handle events? If so, is there another way to structure my object so that it will automatically resize and handle its own events?

My subclass code follows. I can add the frame and app classes as well if necessary.

package my_sizer;
use base 'Wx::StaticBoxSizer';
use Wx qw(:sizer);
use Wx::Event qw(EVT_BUTTON);

sub new {
    my $ref = shift;
    my $parent = shift;

    my $self = $ref->SUPER::new(
        Wx::StaticBox->new($parent, -1, 'Box label'),
        wxHORIZONTAL
    );

    my $button = Wx::Button->new($parent, -1, 'Button');
    $self->Add($button);
    EVT_BUTTON($self, $button, \&click);

    $self->SetSizeHints($parent);

    return $self;
}

sub click { Wx::MessageBox('Click!'); }

Thanks

A: 

My question was answered on http://perlmonks.org.

A sizer is not really suitable for sub-classing as a custom control. Instead, I needed to create my own control, which I did by using Wx::Panel as my subclass. I then created a Wx::StaticBoxSizer on my control.

My new code:

package my_control;
use base 'Wx::Panel';
use Wx qw(:sizer :misc :id);
use Wx::Event qw(EVT_BUTTON);

sub new {
    my $ref = shift;
    my $parent = shift;
    my $self = $ref->SUPER::new($parent, wxID_ANY, wxDefaultPosition, wxDefaultSize);

    $self->{sizer} = Wx::StaticBoxSizer->new(
        Wx::StaticBox->new($self, -1, 'Box label'),
        wxHORIZONTAL
    );

    $self->{button} = Wx::Button->new($self, -1, 'Button');
    $self->{sizer}->Add($self->{button}, 1, wxEXPAND | wxALL, 10);
    EVT_BUTTON($self, $self->{button}, \&click);

    $self->SetSizerAndFit($self->{sizer});

    return $self;
}

sub click { Wx::MessageBox('Click!'); }