如何在Wx :: textCtrl中添加换行符

时间:2011-05-05 14:57:53

标签: perl wxperl

我已将RouMao的解决方案编辑到我的代码示例中。


根据this question here on stackoverflow的接受答案,我应该能够通过终止附加'\ n'的文本为Wx :: TextCtrl添加额外的行。

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

不幸的是,这似乎不起作用,如下面的代码所示。 我在Windows XP上运行Strawberry Perl。

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;}

1 个答案:

答案 0 :(得分:3)

根据manual of wxWidget wxTE_MULTILINE样式,只能在控件创建期间设置。所以你需要改变代码如下:

#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); 

它应该有用!