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.


In reply to Re: Win32::GUI::Menu Problem by Util
in thread Win32::GUI::Menu Problem by PerlingTheUK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.