Example: A Windows Forms Application


The example program in Listing D.3 implements a simple Windows Forms application. The HelloWorldForm class inherits from the System.Windows.Forms.Form base class. It adds a text box and a button to the form. When the user clicks the button, a message box displays the text from the text box.

Listing D.3
 package HelloWorldForm; use strict; use PerlNET qw(with AUTOCALL); use namespace "System"; use namespace "System.Windows.Forms"; use namespace "System.Drawing"; =for interface     [extends: Form]     [STAThread]     static void Main();     private field TextBox textBox;     private void Click(any sender, EventArgs Args); =cut sub Main {     my $self = HelloWorldForm->new;     Application->Run($self); } sub HelloWorldForm {     my $this = shift;     with(my $textBox = TextBox->new(),          Text     => "Hello Windows Forms World",          Location => Point->new(16, 24),          Size     => Size->new(360, 20),          TabIndex => 1);     with(my $button1 = Button->new(),          Text     => "Click Me!",          Location => Point->new(256, 64),          Size     => Size->new(120, 40),          TabIndex => 2);     $button->add_Click(EventHandler->new($this, "click")); my $h = SystemInformation->CaptionHeight;     with($this,          Text              => "Hello Windows Forms World",          AutoScaleBaseSize => Size->new(5, 13),          ClientSize        => Size->new(392, 117),          MinimumSize       => Size->new(392, int(117 + $h)),          AcceptButton      => $button1,          textBox           => $textBox);     $this->{Controls}->Add($_) for $textBox, $button; } sub click {     my($this, $sender, $args) = @_;     MessageBox->Show("Text is: '$this->{textBox}->{Text}'"); } 


Programming in the .NET Environment
Programming in the .NET Environment
ISBN: 0201770180
EAN: 2147483647
Year: 2002
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net