|
From: Mattia B. <mb...@ds...> - 2001-10-26 08:33:08
|
On Fri, 26 Oct 2001, Paul Schabl wrote:
>Hi,
>
>i'm just starting using wxPerl and have the following problem:
>in the code below, in the package myFrame, i have a tree and a
>textcontrol and want to print
>some data into the textcontrol each time an item in the treecontrol is
>selected (e.g. directory-listing);
>how can i access the textcontrol from within the sub OnSelChange in
>package myTreeCtrl ?
>myFrame->TextCtrl->AppendText() doesn't work.
This is because you do not have a 'TextCtrl' method in the
myFrame class; second even if you had it, you need to call it
on an instance of the class .
in the OnSelCHange method add:
my $frame = $this->GetParent();
my $textctrl = $this->{TEXTCTRL}; # see below
$textctrl->AppendText( 'Hi!' );
# of course you could do
# $this->GetParent->{TEXTCTRL}->AppendText( 'Hi!' );
in addition you can add a TextCtrl method to myFrame, like this
sub TextCtrl { $_[0]->{TEXTCTRL} }
and use $frame->TextCtrl instead of $frame->{TEXTCTRL}
if you feel that this makes your code cleaner.
Yet another solution is to pass the TextCtrl to the myTreeCtrl
constructor
i.e.
package myTreeCtrl;
# ...
sub new {
my $class = shift;
my $textctrl = shift;
my $this = $class->SUPER::new( @_ );
$this->{TEXTCTRL} = $textctrl;
}
and use it as myTreeCtrl->new( $textctrl, @other_parameters );
this avoids depending on the fact that the textctrl and the treectrl are
childs of the frame ( you might subsequntly change the layout and
have to change all calls to ->GetParent() to something else.
Regards
Mattia
|