我已将柔毛的解决方案编辑到下面的代码示例中。
根据在stackoverflow上对这个问题的公认答案,我应该能够通过用'n'结束文本来向Wx::TextCtrl添加额外的行。
my $t = Wx::TextCtrl->new( $p, -1, "$titlen" ,[-1,-1],[600,260]);
$t->{TERM}->AppendText( "another linen");
不幸的是,似乎不起作用,如下面的代码所示。我在Windows XP上运行草莓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
, "$titlen"
, [-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 linen");}
sub cancel {exit;}
根据wxWidget手册wxTE_MULTILINE
样式只能在控件创建时设置。因此,您需要更改代码如下:
#my $term = Wx::TextCtrl->new( $p, -1, "$titlen" ,[-1,-1],[600,260]);
#$term->SetWindowStyle(wxTE_MULTILINE);
my $term = Wx::TextCtrl->new( $p, -1, "$titlen" ,[-1,-1],[600,260], wxTE_MULTILINE);
它应该工作!