Question

I've edited RouMao's solution into my code sample below.


according to the accepted answer to this question here on stackoverflow, I should be able to add additional lines to a Wx::TextCtrl by terminating the text appended with '\n'.

my $t = Wx::TextCtrl->new( $p, -1, "$title\n"  ,[-1,-1],[600,260]);
$t->{TERM}->AppendText( "another line\n");

unfortunately this doesn't seem to work, as the following code shows. I'm running Strawberry Perl on Windows XP.

package main;
use Modern::Perl;
WxMultiLineCtrl->new()->MainLoop();    

package WxMultiLineCtrl;

use base qw(Wx::App);
use Wx qw (wxVERTICAL wxTOP wxGROW wxHORIZONTAL wxTE_MULTILINE 
           wxFIXED_MINSIZE wxLEFT );

use Wx::Event qw( EVT_BUTTON );

sub OnInit {

    my $app =  shift ;

    my $title='MultiLine Wx Text Control';
    my $frame = Wx::Frame->new(  undef  ,-1,$title,[-1, -1],[640,280]);
    my $p = Wx::Panel->new(    $frame, -1);
    my $v0= Wx::BoxSizer->new(wxVERTICAL);
    my $h1= Wx::BoxSizer->new(wxHORIZONTAL);
    my $h2= Wx::BoxSizer->new(wxHORIZONTAL);
    my $term      = Wx::TextCtrl->new(  $p, -1
                                      , "$title\n"  
                                      , [-1,-1],[600,260]
                                      , wxTE_MULTILINE
                                     );
    my $cancelBtn = Wx::Button->new(   $p, -1, "cancel"  ,[-1,-1],[-1,-1]);
    my $addTxtBtn = Wx::Button->new(   $p, -1, "add text",[-1,-1],[-1,-1]);
    $p->{TERM}=$term;
    EVT_BUTTON(  $p, $cancelBtn, \&cancel ); 
    EVT_BUTTON(  $p, $addTxtBtn, \&addTxt ); 
    $v0->Add($h1,1,wxLEFT);
    $v0->Add($h2,1,wxLEFT);
    $h1->Add( $term      , 1, wxTOP | wxGROW          , 5 );
    $h2->Add( $cancelBtn , 1, wxTOP | wxFIXED_MINSIZE , 5 );
    $h2->Add( $addTxtBtn , 1, wxTOP | wxFIXED_MINSIZE , 5 );
    $p->SetSizer($v0);
    $p->SetAutoLayout(1);
    $app->SetTopWindow($frame);
    $frame->Show(1);           
}

sub addTxt {shift->{TERM}->AppendText( "another line\n");}
sub cancel {exit;}
Was it helpful?

Solution

According to the manual of wxWidget wxTE_MULTILINE style can be only set during control creation. So you need to change the code as below:

#my $term      = Wx::TextCtrl->new( $p, -1, "$title\n"  ,[-1,-1],[600,260]);
#$term->SetWindowStyle(wxTE_MULTILINE);
my $term = Wx::TextCtrl->new( $p, -1, "$title\n"  ,[-1,-1],[600,260], wxTE_MULTILINE); 

It should work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top