Solved

PopupMenu on a modal form doesnt work

Posted on 2000-03-31
8
474 Views
Last Modified: 2012-06-21
i am developing a SDI application using a lot the PopupMenu method and i notice that on the main form it works very well but when another form is called modally the popupmenu method is executed but nothing happens, i changed the call of this second form for not modally and everything worked so good.
For test just create an module and two form, set start to a sub main, call the form1.show [vbModal] on the sub Main and create something for call the popupmenu method, call form2.show and do something with popupmenu. Now change the call of form2 to vbmodal and watch what happens.
Does anyone know why it is like this???
0
Comment
Question by:Feres
8 Comments
 
LVL 6

Accepted Solution

by:
VBGuru earned 50 total points
Comment Utility
From the help

When you display a pop-up menu, the code following the call to the PopupMenu method isn't executed until the user either chooses a command from the menu (in which case the code for that command's Click event is executed before the code following the PopupMenu statement) or cancels the menu. In addition, only one pop-up menu can be displayed at a time; therefore, calls to this method are ignored if a pop-up menu is already displayed or if a pull-down menu is open.

When a call is made to popupmenu and by selecting an option from it and that option if it opens a modal form. then the call to the popupmenu doesn't work.


Alternative is the use of APIs to create the popupo menus

Here is an example


Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Form_Load()
    'Create an empty popupmenu
    hMenu = CreatePopupMenu()
    'Append a few menu items
    AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
    AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
    AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
    AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim Pt As POINTAPI
    'Get the position of the mouse cursor
    GetCursorPos Pt
    If Button = 1 Then
        'Show our popupmenu
        TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
    Else
        'Show our form's default popup menu
        TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
    End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Destroy our menu
    DestroyMenu hMenu
End Sub

0
 
LVL 6

Expert Comment

by:VBGuru
Comment Utility
even I faced that problem in my project
0
 
LVL 10

Expert Comment

by:caraf_g
Comment Utility
I'm showing popupmenus on modal forms without problems. Must be something else.
0
 
LVL 1

Expert Comment

by:bpoff
Comment Utility
This is a known bug.  You can find a work-around from MS at:

http://support.microsoft.com/support/kb/articles/Q167/8/39.asp
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:VBGuru
Comment Utility
from the form1s popup menu option open a form2, then in form2 try to execute the popupmenu command.

In form1

Form_Click
popupMenu mnuFile
End Sub

mnuForm2_Click

form2.show vbModal

End sub

In form2

form_Click
popupmenu mnuForm2sfile
end sub


Here the form2's popupmenu doesn't come.
0
 

Author Comment

by:Feres
Comment Utility
that's exactly what i wanted to know, thanks a lot VBGuru.
just one ask:
where is this 'From the help', isn't at msdn help, right? if so, you just made me feel like a child, cause i didn´t find it.
0
 
LVL 6

Expert Comment

by:VBGuru
Comment Utility
its in a vb5 help as remarks section

PopupMenu Method

See Also         Example         Applies To         Specifics



Displays a pop-up menu on an MDIForm or Form object at the current mouse location or at specified coordinates. Doesn't support named arguments.

Syntax

object.PopupMenu menuname, flags, x, y, boldcommand



The PopupMenu method syntax has these parts:

Part      Description

object      Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the form with the focus is assumed to be object.
Menuname      Required. The name of the pop-up menu to be displayed. The specified menu must have at least one submenu.
Flags      Optional. A value or constant that specifies the location and behavior of a pop-up menu, as described in Settings.
X      Optional. Specifies the x-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
Y      Optional. Specifies the y-coordinate where the pop-up menu is displayed. If omitted, the mouse coordinate is used.
boldcommand      Optional. Specifies the name of a menu control in the pop-up menu to display its caption in bold text. If omitted, no controls in the pop-up menu appear in bold.


Settings

The settings for flags are:

Constant (location)      Value      Description

vbPopupMenuLeftAlign      0      (Default) The left side of the pop-up menu is located at x.
vbPopupMenuCenterAlign      4      The pop-up menu is centered at x.
vbPopupMenuRightAlign      8      The right side of the pop-up menu is located at x.


Constant (behavior)      Value      Description

vbPopupMenuLeftButton      0      (Default) An item on the pop-up menu reacts to a mouse click only when you use the left mouse button.
vbPopupMenuRightButton      2      An item on the pop-up menu reacts to a mouse click when you use either the right or the left mouse button.


Note   The flags parameter has no effect on applications running under Microsoft Windows version 3.0 or earlier. To specify two flags, combine one constant from each group using the Or operator.



Remarks

These constants are listed in the Visual Basic (VB) object library in the Object Browser.
You specify the unit of measure for the x and y coordinates using the ScaleMode property. The x and y coordinates define where the pop-up is displayed relative to the specified form. If the x and y coordinates aren't included, the pop-up menu is displayed at the current location of the mouse pointer.
When you display a pop-up menu, the code following the call to the PopupMenu method isn't executed until the user either chooses a command from the menu (in which case the code for that command's Click event is executed before the code following the PopupMenu statement) or cancels the menu. In addition, only one pop-up menu can be displayed at a time; therefore, calls to this method are ignored if a pop-up menu is already displayed or if a pull-down menu is open.

0
 

Author Comment

by:Feres
Comment Utility
hi, vbguru
just something i couldnt realize:
how do i write code for the menu i created dynamic??
have u tried using the menu of the form to be displayed on popup (i used getmenu, getsubmenu, TrackPopupMenu) but it only works when the menu is set to visible.
thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now