David,

You should really only have one button created with the -ok option in a window. Even if you have many, the Enter key press would only be sent to the first button created with the option. Usually the option is used in conjunction with the -cancel option and is used to close the DialogBox when either the Enter or ESC key is pressed. To get the functionality that you are looking for, I suggest using a KeyDown event for the textfields and test for the Enter key. Here is an example modified from your code sample. Note that the DialogBox has been changed to a Window, since the DialogBox would intercept any Enter key presses before the textfields would get a chance to respond to the event:

#!perl
use strict;
use warnings;

use Win32::GUI qw();
use Win32::GUI::Constants qw(VK_RETURN);

my $W1 = Win32::GUI::Window->new(
    -name   => "W1",
    -title  => "First Window",
    -pos    => [ 100, 100 ],
    -size   => [ 300, 200 ],
);
$W1->AddButton(
    -name   => "ButtonW1",
    -text   => "FirstName",
    -pos    => [ 87, 100 ],
);
$W1->AddButton(
    -name   => "ButtonW2",
    -text   => "LastName",
    -pos    => [ 87, 120 ],
);
$W1->AddTextfield(
    -name   => "tf1",
    -pos    => [20,40],
    -size   => [250,20],
    -prompt => "1:",
);
$W1->AddTextfield(
    -name   => "tf2",
    -pos    => [20,60],
    -size   => [250,20],
    -prompt => "2:",
);
$W1->Show();
print "This is a test\n";
Win32::GUI::Dialog();
exit(0);
sub W1_Terminate { return -1; }
# Keydown event for textfield 1
sub tf1_KeyDown {
    my($flags,$key) = @_;
    if($key == VK_RETURN){
        ButtonW1_Click();
    }
    return 1;
}
# Keydown event for textfield 2
sub tf2_KeyDown {
    my($flags,$key) = @_;
    if($key == VK_RETURN){
        ButtonW2_Click();
    }
    return 1;
}
sub ButtonW1_Click {
    print "Button 1 Clicked\n";
    my $text = $W1->tf1->Text();
    print "$text\n";
}
sub ButtonW2_Click {
    print "Button 2 Clicked\n";
    my $text = $W1->tf2->Text();
    print "$text\n";
}

__END__

Hope this helps,

Kevin.

I'm interested in using two textfields in a DialogBox. Each textfield needs to except input from the enter key. I have learned that when you create your first button with -ok => 1, this becomes the button that will click regardless of the textbox you are in.

I'm including some code I'm playing around with:
When you run this and enter 1 for TextBox 1 and 2 for TextBox 2, Then click on FirstName Button - The result is Button 1 Clicked and a 1 on the next line. This is Expected. Then click on LastName Button - The result is Button 2 Clicked and a 2 on the next line. This is Expected. When you go into TextBox1 and click enter the result is "Button 1 Clicked" and a 1 on the next line. This is Expected. When you go into TextBox2 and click enter the result is This is NOT Expected.

How do I get the result "Button 2 Clicked" and a 2 on the next line, when I go into textbox2 and click ? It seems the -ok => 1 only works for the first button created. Any suggestions. Thanks.
Dave
use Win32::GUI;
my $W1 = Win32::GUI::DialogBox->new(
-name => "W1",
-title => "First Window",
-pos => [ 100, 100 ],
-size => [ 300, 200 ], );
$W1->AddButton( -name => "ButtonW1",
-text => "FirstName",
-pos => [ 87, 100 ],
-ok => 1,
              );
$W1->AddButton( -name => "ButtonW2",
-text => "LastName",
-pos => [ 87, 120 ],
-ok => 1,
              );
# $W1->ButtonW1->Disable();
$W1->AddTextfield( -name => "tf1",
-left => 20,
-top => 40,
-width => 250,
-height => 20, -prompt => "1:",
);
$W1->AddTextfield(  -name     => "tf2",
                    -left     =>  20,
                    -top      =>  60,
                    -width    => 250,
                    -height   => 20,
                    -prompt   => "2:",
                );
$W1->Show(); print "This is a test\n"; Win32::GUI::Dialog(); exit(0);
sub W1_Terminate { return -1; }
sub ButtonW1_Click {
print "Button 1 Clicked\n"; my $text = $W1->tf1->Text();
print "$text\n"; }
sub ButtonW2_Click {
print "Button 2 Clicked\n"; my $text = $W1->tf2->Text();
print "$text\n"; }


------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d


_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

------------------------------------------------------------------------------
Colocation vs. Managed Hosting
A question and answer guide to determining the best fit
for your organization - today and in the future.
http://p.sf.net/sfu/internap-sfd2d
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Reply via email to