I did not try your code on its own because I'm sure it does work when kept the way it is.
But when I tried to implement it such that it was all in the main loop, things didn't improve.
So it must be some sort of side effect from using the ListIcon gadget (or some option like the one that allows the entire row to be selected) because in my other project which uses a ListView gadget instead never has an issue with the code I pasted earlier.
I can appreciate your not recommending the inner event loop (merely for the popup menu).
Despite this, when I noticed that I didn't really have a Repeat .. Until loop, I went ahead and introduced it so that the inner loop would continue looping until a Menu Event occurred.
Things started working, but sure enough you were right. I discovered that if a user Cancels the Popup Menu by clicking off of it (not making a selection), the menu goes away but then the program is stuck in the inner loop! Thus the user can't click the close button, etc.
I was about to remove the inner event loop but I really like it because it makes it possible for me to confine the menu choices to 1, 2, 3, ... thus I don't have to use the Enumeration .. EndEnumeration or anything fancy to ensure that I'm using unique menu item #'s etc.
Keep in mind that everything worked. The only caveat was that if the user doesn't make a choice and clicks off the menu, it goes away and the program gets stuck in the inner loop!
So I got to thinking. What if I had a timer? But that seemed a little bit much.
So I used a counter! I now Repeat .. Until a menu event occurs, or the counter reaches a specific value.
This gives inner event loop enough time to determine which choice the user selected from the popup menu but it also ensures that control is returned to the outer (main) event loop just as soon as the popup menu disappears!
Here's the code snippet:
In this case the gadget is a ListIcon gadget with many rows of emails listed...
Code: Select all
If Event = #PB_Event_Gadget And (EventType = #PB_EventType_RightClick)
Select GadgetID
Case #Email:
menuRes = CreatePopupMenu(#PB_Any) ;pop-up menu...
If menuRes
menuPos = GetGadgetState(#Email) ;get which row is involved
;make sure row clicked on has content!
If menuPos > -1 And menuPos < CountGadgetItems(#Email)
MenuItem(1, "Forward")
MenuItem(2, "Delete")
DisplayPopupMenu(menuRes,WindowID(w1))
tries = 0 ;ENSURE THAT WE RETURN TO OUTER EVENT LOOP
Repeat
Event = WaitWindowEvent()
EventType = EventType()
Select Event
Case #PB_Event_Menu ; a popup-menu item was clicked
Select EventMenu() ; get the clicked menu item...
Case 1 :
id = Val(GetGadgetItemText(#Email,menuPos,0))
ForwardEmail(id)
Case 2 :
id = Val(GetGadgetItemText(#Email,menuPos,0) )
If DeleteEmail(id)
RemoveGadgetItem(#Email,menuPos)
EndIf
EndSelect ;EventMenu()
EndSelect ;WaitWindowEvent()
;THIS IS WHAT ENSURES THAT WE RETURN TO OUTER EVENT LOOP!!
tries + 1
Until Event = #PB_Event_Menu Or EventType = PB_EventType_LeftClick Or tries > 5
EndIf ;menuPos > -1 and menuPos < countGadgetItems(#Email)...
EndIf ;menuRes
This seems to be working well with the ListIcon gadget, sans side effects!
Thanks again,
Roark