Good tip. I’ve just now edited the example in the help page for this function to make that more clear (it was mentioned in the text, but not really highlighted as it should have been). I also took the opportunity to show how you can use the menu( function to create more complex menus. The new text won’t appear in the help until the next release, but here I’ve included it below:
This statement can be used to produce a pop-up menu at the current mouse position. It is intended that this statement be used in a procedure that is triggered by a button on a form (or any form object that triggers a procedure). The procedure must be triggered on mouse down, not mouse up.
Here is an example that displays a pop-up menu with three colors when the button is clicked.
local colorChoice
popupclick "Red"+cr()+"Green"+cr()+"Blue","Red",colorChoice
if colorChoice = "" return endif // do nothing if user pulled off mouse
// colorChoice now contains the color name
if colorChoice="Red"
.. do something redish
elseif colorChoice="Green"
.. do something greenish
elseif colorChoice="Blue"
.. do something blueish
endif
You can also use the [menu(] and [menuitem(] functions to create the menu, as shown in this example. This is more complicated but gives you more control – you can give menu items different colors or styles, or even include submenus in your pop-up menu.
local colorChoice
let colorMenu = menu("Colors")
menuitem("Red","color","770000")+
menuitem("Green","color","007700")+
menuitem("Blue","color","000077")
popupclick colorMenu,"Red",colorChoice
if colorChoice = "" return endif // do nothing if user pulled off mouse
// colorChoice now contains the color name
if colorChoice="Red"
.. do something redish
elseif colorChoice="Green"
.. do something greenish
elseif colorChoice="Blue"
.. do something blueish
endif
Oh, you had me scared for a moment, thought there was a bug. “Null” has a definite meaning to a C programmer that is different than empty text (“”). So if there are any other C programmers out there, this function doesn’t actually return null, it returns empty text. But Joe is correct, if you plugged the result directly into a field without checking for empty, you would zap the field contents if you don’t check for empty. So don’t put the result directly into a field, put it into a variable, check the variable for empty, then only update the field if not empty. So here is an example of the correct way to do it.
local colorChoice
popupclick "Red"+cr()+"Green"+cr()+"Blue",ColorField,colorChoice
if colorChoice <> ""
ColorField = colorChoice
endif