Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Win32::GUI::Menu Problem

by PerlingTheUK (Hermit)
on Oct 19, 2005 at 07:49 UTC ( [id://501207]=perlquestion: print w/replies, xml ) Need Help??

PerlingTheUK has asked for the wisdom of the Perl Monks concerning the following question:

Dearest Monks,

After having used TK and growing into loving it, I have recently been asked to change some applications into the Win32::GUI which is reasonably new for me.
I have the following problem:

# Menu setup my $menuMW = $ptrMainWindow->AddMenu( -pos => [ 10, 10 ], -size=> [ 30, 30 ] ); my $btnMenu = $menuMW->AddMenuButton( -text => "Configuration"); my $itmDBSetup = $btnMenu->AddMenuItem( -text => "Database Setup", -onClick => sub{ displayDlgDBSetup( $ptrMainWindow ) } ); my $itmQuit = $btnMenu->AddMenuItem( -text => "Exit" );
The program works fine until I add a second menu item. All menu items then apply the -onClick Event of the item added later or as in the example above, none at all.

Cheers,
PerlingTheUK

Replies are listed 'Best First'.
Re: Win32::GUI::Menu Problem
by Util (Priest) on Oct 19, 2005 at 18:26 UTC

    Add the -id option to your MenuItems. In the code below, with -id commented out, the program behaves as you described, with the added bug of reversing the order of Database Setup and Exit. If the -id lines are uncommented, then the program behaves properly.

    use strict; use warnings; use Win32::GUI; my $next_menu_id = 1; my $menuMW = Win32::GUI::Menu->new(); my $btnMenu = $menuMW->AddMenuButton( -text => 'Configuration', # -id => $next_menu_id++, ); my $itmDBSetup = $btnMenu->AddMenuItem( -text => 'Database Setup', # -id => $next_menu_id++, -onClick => sub { print "I'm the first OnClick!\n"; }, ); my $itmQuit = $btnMenu->AddMenuItem( -text => 'Exit', # -id => $next_menu_id++, -onClick => sub { print "I'm the second OnClick, and I Quit!\n"; return -1; }, ); my $MainWindow = GUI::Window->new( -text => 'Main', -name => 'MainWindow', -pos => [ 10, 10 ], -size => [ 900, 900 ], -menu => $menuMW, ); $MainWindow->Show(); Win32::GUI::Dialog(); sub MainWindow_Terminate { return -1; }

    Yet Another (better, but woefully underdocumented) Way To Do It:

    use strict; use warnings; use Win32::GUI; # Adapted from: # http://www.perlmonks.org/index.pl?node_id=18472 my $menuMW = Win32::GUI::Menu->new( '&Configuration' => 'Config', ' > &Database Setup' => 'DB_Setup', ' > E&xit' => 'Exit', 'Second&TopLevel' => 'STL', ' > &foo' => 'Foo', ' > &bar' => 'Bar', ); my $MainWindow = GUI::Window->new( -text => 'Main', -name => 'MainWindow', -pos => [ 10, 10 ], -size => [ 900, 900 ], -menu => $menuMW, ); $MainWindow->Show(); Win32::GUI::Dialog(); sub MainWindow_Terminate { return -1; } sub DB_Setup_Click { print "DB_Setup.\n"; # displayDlgDBSetup( $MainWindow ); } sub Exit_Click { print "I am exiting via the 'Exit' menu option.\n"; return -1; } sub Foo_Click { print "Foo.\n"; } sub Bar_Click { print "Bar.\n"; }
    Using this method, Win32::GUI::Menu internally calls MakeMenu, which automatically handles assigning -id. You cannot use OnClick with this method, though; you must use FOO_Click event procedures.

      Even with this "woefully underdocumented" way to do it, you can still use "-onClick" :
      my $menu = Win32::GUI::Menu->new( "Title text" => "Title name" , "> Opt 1 text" => { -name => "opt1_name", -onClick => \&function +_for_opt1 } , "> -" => 0 # separator , "> Opt 2 text" => { -name => "opt2_name", -onClick => sub{ DoSom +ething(); DoAnotherThing(); } } );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://501207]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found