You have a scoping problem.  At the very least, you have to move the my 
$popup_menu out of the ListView_RightClick sub, and put it somewhere before 
your Win32::GUI::Dialog() call.   You can either a) just move the declaration 
of the variable and leave the assignment where it is in the ListView_RightClick 
sub or, move the entire declaration and assignment before the Dialog phase.    
 
The problem is that during compilation, Item1_Click is parsed, but there is no 
defined Item1 click at that time so it basically becomse unreachable memory (I 
have no idea how the innards work, but am going off a logical thought process). 
   
 
See my example:
 
use strict; 
use Win32::GUI; 
 
my $mainwindow; 
create_mainwindow (); 
$mainwindow ->Show(); 
my $popup_menu;
Win32::GUI::Dialog; 
 
sub create_mainwindow  { 
 
        my $window_width = 400; 
        my $window_height = 400; 
 
        $mainwindow = Win32::GUI::Window -> new ( 
                -title                => "Foo", 
                -name                => "Main", 
                -width                => $window_width, 
                -height                => $window_height, 
        ); 
 
        $mainwindow -> AddListView  ( 
                -name                => "ListView", 
                -top                => 0, 
                -left                => 0, 
                -width                => $window_width - 10, 
                -height                => $window_height - 30, 
                -style          => WS_CHILD | WS_VISIBLE | WS_VSCROLL | 1, 
                -fullrowselect  => 1, 
        ); 
 
        $mainwindow -> ListView -> InsertColumn ( 
                -index                => 0, 
                -width                => 150, 
                -text                => "Column 1", 
        ); 
 
        $mainwindow -> ListView -> InsertColumn ( 
                -index                => 1, 
                -width                => 150, 
                -text                => "Column 2", 
        ); 
} 
sub ListView_RightClick  { 
        $popup_menu = Win32::GUI::MakeMenu ( 
                    "Foo"                => "Foo", 
                    " > Item 1"        => "Item1", 
                    " > Item 2"        => "Item2", 
                    " > Item 3"        => "Item3",                  ### Added 
the new menu value
        );
 

        eval qq|sub Item3_Click {print "Debug - Item 3\n"}|;   #### here is my 
eval 
              ### I used pipes for my quote operator because 
              ### string eval needs a "string" 

 
          
        my ( $x, $y ) = Win32::GUI::GetCursorPos (); 
        $mainwindow->TrackPopupMenu ( $popup_menu -> {Foo}, $x, $y ); 
} 
 
sub Item1_Click  { 
 
        print "DEBUG - Item 1\n"; 
 
} 
 
sub Item2_Click  { 
 
        print "DEBUG - Item 2\n"; 
 
} 
 
sub Main_Terminate  { 
 
        return -1; 
 
} 

 
If you want your menu to be dynamic (ie, built at run type based on an if/else 
statement), you will have to use string eval to "parse" the the event handlers 
in the same call stack as the ListView_RightClick.   I use this to build a 
"filter" tool bar menu for one of the apps I built where there are a number 
databases residing on several servers.  The list of servers and databases they 
are on comes from another database, so if a new "child" database is added, and 
it happens to be on a new server, my code does not have to be changed.   I also 
build up my toolbar menu dynamically at runtime (during load).  
 
Joe Frazier, Jr.
Senior Support Engineer

Peopleclick Service Support
Tel:  +1-800-841-2365
E-Mail: [EMAIL PROTECTED] 
 


________________________________

        From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL 
PROTECTED]
        Sent: Monday, May 09, 2005 17:28
        To: perl-win32-gui-users@lists.sourceforge.net
        Subject: [perl-win32-gui-users] Right Click Menu
        
        

        I was wondering if anyone could help me with a little issue I am 
having. 
        
        I am writting an application however when I try to click on some 
buttons that I have defined, it just hangs the script.  I have no idea why it 
does this, it just does. 
        
        Anyway, I decided to do away with the buttons and just go with a 
Right-Click menu, however I am having problems getting that to work.  The 
following is my code which I would expect to print out some statements, however 
when I select either Item 1 or Item 2 nothing happens. 
        
        Can someone please help me out. 
        
        Also, I have tried this on two machines, one running 5.6.1 with .588 
and the other running 5.8.6 running 1.0, however because of the RichEdit crash 
on exit I would really like this to run on .588. 
        
        Thanks. 
        
        Len. 
        
        --------------------------[ Begin Code ]------------------------------- 
        
        use strict; 
        
        use Win32::GUI; 
        
        my $mainwindow; 
        
        create_mainwindow (); 
        
        $mainwindow -> Show(); 
        
        Win32::GUI::Dialog; 
        
        sub create_mainwindow  { 
        
                my $window_width = 400; 
                my $window_height = 400; 
        
                $mainwindow = Win32::GUI::Window -> new ( 
                        -title                => "Foo", 
                        -name                => "Main", 
                        -width                => $window_width, 
                        -height                => $window_height, 
                ); 
        
                $mainwindow -> AddListView  ( 
                        -name                => "ListView", 
                        -top                => 0, 
                        -left                => 0, 
                        -width                => $window_width - 10, 
                        -height                => $window_height - 30, 
                        -style          => WS_CHILD | WS_VISIBLE | WS_VSCROLL | 
1, 
                        -fullrowselect  => 1, 
                ); 
        
                $mainwindow -> ListView -> InsertColumn ( 
                        -index                => 0, 
                        -width                => 150, 
                        -text                => "Column 1", 
                ); 
        
                $mainwindow -> ListView -> InsertColumn ( 
                        -index                => 1, 
                        -width                => 150, 
                        -text                => "Column 2", 
                ); 
        
        } 
        
        sub ListView_RightClick  { 
        
                my $popup_menu = Win32::GUI::MakeMenu ( 
                        "Foo"                => "Foo", 
                        " > Item 1"        => "Item1", 
                        " > Item 2"        => "Item2", 
                ); 
        
                my ( $x, $y ) = Win32::GUI::GetCursorPos (); 
        
                $mainwindow -> TrackPopupMenu ( $popup_menu -> {Foo}, $x, $y ); 
        
        } 
        
        sub Item1_Click  { 
        
                print "DEBUG - Item 1\n"; 
        
        } 
        
        sub Item2_Click  { 
        
                print "DEBUG - Item 2\n"; 
        
        } 
        
        sub Main_Terminate  { 
        
                return -1; 
        
        } 
        
        --------------------------[ End Code ]------------------------------- 

        
------------------------------------------------------------------------------
        Electronic Privacy Notice. This e-mail, and any attachments, contains 
information that is, or may be, covered by electronic communications privacy 
laws, and is also confidential and proprietary in nature. If you are not the 
intended recipient, please be advised that you are legally prohibited from 
retaining, using, copying, distributing, or otherwise disclosing this 
information in any manner. Instead, please reply to the sender that you have 
received this communication in error, and then immediately delete it. Thank you 
in advance for your cooperation.
        
==============================================================================
        

Reply via email to