Search code examples
perlwxwidgetswxperl

Validation of textbox values in wxPerl crashes perl interpreter


I'm using:

  • Windows 7
  • Strawberry Perl
  • using current version of wxPerl (from CPAN)

The perl code that creates the layout has been generated by wxGlade. This code results in the error "Perl Interpretor has stopped working":

use Wx 0.15 qw[:allclasses];
use strict;

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


package MyFrame;

use Wx;
use Wx qw[:everything];
use Wx::Event qw( EVT_BUTTON EVT_CLOSE );
use Wx::Perl::TextValidator;
use base qw(Wx::Frame Class::Accessor::Fast);
use strict;

use Wx::Locale gettext => '_T';

__PACKAGE__->mk_ro_accessors( qw(numeric string) );

sub new {
my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;
$parent = undef              unless defined $parent;
$id     = -1                 unless defined $id;
$title  = ""                 unless defined $title;
$pos    = wxDefaultPosition  unless defined $pos;
$size   = wxDefaultSize      unless defined $size;
$name   = ""                 unless defined $name;

# begin wxGlade: MyFrame::new
$style = wxDEFAULT_FRAME_STYLE 
    unless defined $style;
my $numval = Wx::Perl::TextValidator->new( '\d' );

$self = $self->SUPER::new( $parent, $id, $title, $pos, $size, $style, $name );
$self->{text_ctrl_1} = Wx::TextCtrl->new($self, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, );
$self->{button_1} = Wx::Button->new($self, wxID_ANY, _T("Get"));
$self->{label_1} = Wx::StaticText->new($self, wxID_ANY, _T("From:"), wxDefaultPosition, wxDefaultSize, );
$self->{text_ctrl_2} = $self->{numeric} = Wx::TextCtrl->new($self, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, );
$self->{label_2} = Wx::StaticText->new($self, wxID_ANY, _T("To:    "), wxDefaultPosition, wxDefaultSize, );
$self->{text_ctrl_3} = $self->{numeric} = Wx::TextCtrl->new($self, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, );
$self->{radio_box_1} = Wx::RadioBox->new($self, wxID_ANY, _T("Vote?"), wxDefaultPosition, wxDefaultSize, [_T("Yes"), _T("No")], 2, wxRA_SPECIFY_ROWS);
$self->{text_ctrl_4} = Wx::TextCtrl->new($self, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);

$self->__set_properties();
$self->__do_layout();

# end wxGlade

$self->{text_ctrl_2}->SetValidator ( $numval ); #<- this is where the program crashes
$self->{text_ctrl_3}->SetValidator ( $numval ); #<- this WORKS actually

EVT_BUTTON(
    $self,
    $self->{button_1},
    \&GetURL
    );

EVT_CLOSE(
    $self,
    \&OnClose
    );

return $self;
}

I had no errors prior to trying the number validation on those two textctrl's. What I'm trying to do is accept only digits in my fields.

I'm using wxperl_demo as my documentation.

The fact that the second SetValidator is working is curious to me, what could be the problem?


Solution

  • Apparently, you can't use the same variable twice as an argument to SetValidator. Using another one solved this.