From: boo r. <boo...@at...> - 2002-10-23 17:06:58
|
Michal Jurosz wrote: > How define the same OnChar event for several textctrls ? > > I can do this with > > EVT_CHAR( $self->FindWindow($main::ID_CHILD1), \&OnChar ); > EVT_CHAR( $self->FindWindow($main::ID_CHILD2), \&OnChar ); > .... > EVT_CHAR( $self->FindWindow($main::ID_CHILD25), \&OnChar ); > > but it is so long ... can I do the same thing easier ? You may want to consider subclassing the text control and setting up the EVT_CHAR in the constructor. This would probably be easier in the long run, but if that doesn't appeal to you, you could try something like the following snippet. Lines 43-46 show how to add items and event handlers for things stored in a hash (although you'll have to make sure they get ordered correctly on your own) and then lines 51-54 demonstrate how by storing the created text controls in an array, and then using foreach to create the handlers in one go. #!/usr/bin/perl -w use Wx; package MyApp; use strict; use vars qw(@ISA); @ISA=qw(Wx::App); use Wx qw(wxDefaultSize wxDefaultPosition); sub OnInit { my( $this ) = @_; my( $dialog ) = aDialog->new( "text controls",wxDefaultPosition); $this->SetTopWindow( $dialog ); $dialog->Show(1); 1; } package aDialog; use strict; use vars qw(@ISA); @ISA=qw(Wx::Dialog); use File::Find; use File::Copy; use File::Path; use Wx::Event qw(EVT_TEXT EVT_CLOSE); use Wx qw(wxDefaultSize wxDefaultValidator wxALIGN_RIGHT wxID_CANCEL); use Win32::DriveInfo; sub new { my( $class ) = shift; my( $this ) = $class->SUPER::new( undef, -1, $_[0], $_[1], [510, 510] ); my @textitems; my $ypos = 1; my %misc = ( "User ID" => "", "First Name" => "John", "Last Name" => "Doe" ); # populate the items in hash # add event handlers right away foreach (keys %misc) { my $tempTextCtrl = $this->{$_} = Wx::TextCtrl->new( $this, -1, $misc{$_}, [20, $ypos++ * 20], [340, -1] ); EVT_TEXT ($this, $tempTextCtrl, \&onUpdateText); } # now populate some more items # add event handlers after items are added to the main dialog push @textitems, $this->{text1} =Wx::TextCtrl->new( $this, -1, "foo", [20, $ypos++ * 20], [340, -1] ); push @textitems, $this->{text2} =Wx::TextCtrl->new( $this, -1, "bar", [20, $ypos++ * 20], [340, -1] ); push @textitems, $this->{text3} =Wx::TextCtrl->new( $this, -1, "baz", [20, $ypos++ * 20], [340, -1] ); EVT_TEXT ($this, $_, \&onUpdateText) foreach @textitems; EVT_CLOSE( $this, \&OnClose ); $this; } sub onUpdateText { my ($this, $event) = @_; print "hello!" } sub OnClose { my( $this, $event ) = @_; $this->Destroy(); } package main; my($app) = MyApp->new(); $app->MainLoop(); |