From: Frazier, J. J. <Joe.Frazier@Peopleclick.com> - 2005-05-10 12:18:35
|
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. =20 =20 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). =20 =20 See my example: =20 use strict;=20 use Win32::GUI;=20 =20 my $mainwindow;=20 create_mainwindow ();=20 $mainwindow ->Show();=20 my $popup_menu; Win32::GUI::Dialog;=20 =20 sub create_mainwindow {=20 =20 my $window_width =3D 400;=20 my $window_height =3D 400;=20 =20 $mainwindow =3D Win32::GUI::Window -> new (=20 -title =3D> "Foo",=20 -name =3D> "Main",=20 -width =3D> $window_width,=20 -height =3D> $window_height,=20 );=20 =20 $mainwindow -> AddListView (=20 -name =3D> "ListView",=20 -top =3D> 0,=20 -left =3D> 0,=20 -width =3D> $window_width - 10,=20 -height =3D> $window_height - 30,=20 -style =3D> WS_CHILD | WS_VISIBLE | WS_VSCROLL = | 1,=20 -fullrowselect =3D> 1,=20 );=20 =20 $mainwindow -> ListView -> InsertColumn (=20 -index =3D> 0,=20 -width =3D> 150,=20 -text =3D> "Column 1",=20 );=20 =20 $mainwindow -> ListView -> InsertColumn (=20 -index =3D> 1,=20 -width =3D> 150,=20 -text =3D> "Column 2",=20 );=20 }=20 sub ListView_RightClick {=20 $popup_menu =3D Win32::GUI::MakeMenu (=20 "Foo" =3D> "Foo",=20 " > Item 1" =3D> "Item1",=20 " > Item 2" =3D> "Item2",=20 " > Item 3" =3D> "Item3", = ### Added the new menu value ); =20 eval qq|sub Item3_Click {print "Debug - Item 3\n"}|; #### here = is my eval=20 ### I used pipes for my quote operator because=20 ### string eval needs a "string"=20 =20 =20 my ( $x, $y ) =3D Win32::GUI::GetCursorPos ();=20 $mainwindow->TrackPopupMenu ( $popup_menu -> {Foo}, $x, $y );=20 }=20 =20 sub Item1_Click {=20 =20 print "DEBUG - Item 1\n";=20 =20 }=20 =20 sub Item2_Click {=20 =20 print "DEBUG - Item 2\n";=20 =20 }=20 =20 sub Main_Terminate {=20 =20 return -1;=20 =20 }=20 =20 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). =20 =20 Joe Frazier, Jr. Senior Support Engineer Peopleclick Service Support Tel: +1-800-841-2365 E-Mail: Support@Peopleclick.com=20 =20 ________________________________ From: per...@li... = [mailto:per...@li...] On Behalf Of = leo...@us... Sent: Monday, May 09, 2005 17:28 To: per...@li... Subject: [perl-win32-gui-users] Right Click Menu =09 =09 I was wondering if anyone could help me with a little issue I am = having.=20 =09 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.=20 =09 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.=20 =09 Can someone please help me out.=20 =09 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.=20 =09 Thanks.=20 =09 Len.=20 =09 --------------------------[ Begin Code ]------------------------------- = =09 use strict;=20 =09 use Win32::GUI;=20 =09 my $mainwindow;=20 =09 create_mainwindow ();=20 =09 $mainwindow -> Show();=20 =09 Win32::GUI::Dialog;=20 =09 sub create_mainwindow {=20 =09 my $window_width =3D 400;=20 my $window_height =3D 400;=20 =09 $mainwindow =3D Win32::GUI::Window -> new (=20 -title =3D> "Foo",=20 -name =3D> "Main",=20 -width =3D> $window_width,=20 -height =3D> $window_height,=20 );=20 =09 $mainwindow -> AddListView (=20 -name =3D> "ListView",=20 -top =3D> 0,=20 -left =3D> 0,=20 -width =3D> $window_width - 10,=20 -height =3D> $window_height - 30,=20 -style =3D> WS_CHILD | WS_VISIBLE | WS_VSCROLL = | 1,=20 -fullrowselect =3D> 1,=20 );=20 =09 $mainwindow -> ListView -> InsertColumn (=20 -index =3D> 0,=20 -width =3D> 150,=20 -text =3D> "Column 1",=20 );=20 =09 $mainwindow -> ListView -> InsertColumn (=20 -index =3D> 1,=20 -width =3D> 150,=20 -text =3D> "Column 2",=20 );=20 =09 }=20 =09 sub ListView_RightClick {=20 =09 my $popup_menu =3D Win32::GUI::MakeMenu (=20 "Foo" =3D> "Foo",=20 " > Item 1" =3D> "Item1",=20 " > Item 2" =3D> "Item2",=20 );=20 =09 my ( $x, $y ) =3D Win32::GUI::GetCursorPos ();=20 =09 $mainwindow -> TrackPopupMenu ( $popup_menu -> {Foo}, $x, $y ); = =09 }=20 =09 sub Item1_Click {=20 =09 print "DEBUG - Item 1\n";=20 =09 }=20 =09 sub Item2_Click {=20 =09 print "DEBUG - Item 2\n";=20 =09 }=20 =09 sub Main_Terminate {=20 =09 return -1;=20 =09 }=20 =09 --------------------------[ End Code ]-------------------------------=20 = -------------------------------------------------------------------------= ----- 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. = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D =09 |