Doug,

 

That will work, but it breaks one of the golden rules of OO programming: don’t access internal object data directly. Of course, it doesn’t help that there isn’t a ‘Name’ method! The same applies to the window handle {-handle}, incidentally.

 

A more purist approach would be to use the UserData method, something like this:

 

##################################################################

use strict;

use Win32::GUI();

 

my $obMW = new Win32::GUI::Window(

            -name                           => 'Main',

            -size                             => [200,200],

            -text                              => 'Main',

);

 

$obMW->AddButton(

            -name                           => 'Button1',

            -pos                              => [10,10],

            -size                             => [75,23],

            -text                              => 'Button1',

            -onClick                        => sub { Win32::MsgBox("You just clicked ".$_[0]->UserData) },

);

 

$obMW->Button1->UserData('Button1');

 

$obMW->AddButton(

            -name                           => 'Button2',

            -pos                              => [10,50],

            -size                             => [75,23],

            -text                              => 'Button2',

            -onClick                        => sub { Win32::MsgBox("You just clicked ".$_[0]->UserData()) },

);

 

$obMW->Button2->UserData('Button2');

 

$obMW->Show();

Win32::GUI::Dialog();

#######################################################################

 

That won’t work for the handle, though, as we don’t have it at control creation time. Perhaps we should add ‘Name’ and ‘Handle’ methods to the module?

 

Glenn

 


From: perl-win32-gui-users-bounces@lists.sourceforge.net [mailto:perl-win32-gui-users-bounces@lists.sourceforge.net] On Behalf Of Doug Hoffman
Sent: 21 March 2008 14:23
To: perl-win32-gui-users@lists.sourceforge.net
Subject: Re: [perl-win32-gui-users] Button onClick event

 

Thanks to all who replied.

Here is the code that I received.

 

my ($ButtonClicked, @other_params) = @_;
my $ButtonName = $ButtonClicked->{-name};


Thanks to Sean for the email
He was nice enough to explain the mechanics of the code above and how it fetches the button -name

 

 

Here's the button add and button_click sub for all who may want it.

 

### add button #########################

my $Button = new Win32::GUI::Button($Main,
-name => "Test",
-tip => "Test",
-width => 15,
-height => 15,
-left => 600,
-onClick => 'Button_Clicked',
);
$Main->AddButton($Button);

####################################

 

####################################
sub Button_Clicked
{

my ($ButtonClicked, @other_params) = @_;
my $ButtonName = $ButtonClicked->{-name};

 

### $ButtonName is now ready for use :) ###

}
#####################################

Thanks,

Doug

 

 

 

 

 

The first item passed into the onClick subroutine is the
Win32::GUI::Button object. (Note: NOT the {-name} of the object, but the
blessed Perl object itself.)

> ### create buttons ###########################################
> $ButtonNumber = new Win32::GUI::Button($Main,
> -name => "$ButtonName",
> -tip => "$ButtonName",
> -pos => [$posX, $posY],
> -size => [15, 15],
> -onClick => 'Button_Clicked',
> ); 


> ### sub that all buttons go to ###
> sub Button_Clicked{

> ?????????????????????????? get button -name here. :P

my ($button, @other_params) = @_;
my $ButtonName = $button->{-name};

> $Main->Status3->Text("$ButtonName");## display button name in status bar
> }
> #########################################################

I can't recall off the top of my head what the other params are, but the
docs should say. (And if all you want is the name, they are not important.)