Determine If PopupMenu Item Already Exists

How do I determine if a PopupMenu already contains a particular item?

I’m getting around it for now by assigning the Popupmenu items to a string() and checking that.

I’m looking for something like…

if myPopupMenu.Has("Free Weights") then ...

The code to use depends (as usual) of many parameters:

a. You fill the PopupMenu at Design time
http://documentation.xojo.com/api/deprecated/popupmenu.html#popupmenu-initialvalue
A scan of this property will return what you want

b. You fill the PopupMenu at run time, from a text file
You can also fill a dictionary (at the PopupMenu filling time), then check the Dictionary contents…
http://documentation.xojo.com/api/language/dictionary.html

c. Other… explain, please.

Have-you read: http://documentation.xojo.com/api/deprecated/popupmenu.html#popupmenu-rowvalueat ?
You may use that inside a loop and test if you find the entry you are seeking.

if you fill at runtime you could subclass your PopupMenu and add own methods.
or with extends you can extends classes with own methods.

This is what I’m doing… filling it at runtime.

I was expecting a simple IndexOf or Contains or Has method on the Popupmenu, but since that’s not available, I’ll just use my simple string array for now.

@Markus Rauch That’s an interesting idea. I’m just getting started with Xojo and this particular project, so I’ll explore that in the future.

Thank you for the feedback!

here you are. not tested but should work.

Public Function Contains(extends aPopup as popupMenu, searchString as String) as Boolean
  '---------------------------------------------------------------------------------
  ' Description : returns true if the searchString is in one of the popupMenu items
  '---------------------------------------------------------------------------------
  
  For i As Integer=0 To aPopup.ListCount-1
    If aPopup.List( i) = searchString Then Return True
  Next
  
  Return False
  
End Function

@Jean-Yves Pochez Thank you for that snippet!

yes, seems that IndexOf if missing there

Here is the Method I wrote that works with both Desktop and Web apps to set a popup menu’s highlighted value. If the Value is missing from the popup menu, it can optionally be added. I sometimes use an ID at the start of a popup string, so I added the option of using the VAL() instead.

[code]Protected Sub doSetPopupMenuValueWAD(myPopupMenu As Object, setAsValue As String, addIfMissing As Boolean = False, valueAsNumber As Boolean = False)
Dim ListCount As Integer
Dim tempString As String

#If TargetDesktop Then
Dim tempPopupMenu As PopupMenu = PopupMenu(myPopupMenu)
#ElseIf TargetWeb Then
Dim tempPopupMenu As WebPopupMenu = WebPopupMenu(myPopupMenu)
#Else
Return
#EndIf

ListCount = tempPopupMenu.ListCount
For tempInt As Integer = 0 To ListCount - 1
tempString = tempPopupMenu.List(tempInt)
If tempString = setAsValue Or (valueAsNumber And tempString.val = setAsValue.val) Then 'found the string in the list
tempPopupMenu.ListIndex = tempInt
Return
End If
Next

If addIfMissing Then
tempPopupMenu.AddRow(ReplaceAll(setAsValue, “&”, “&&”)) 'escape the & else it is used by Windows as a key code!
tempPopupMenu.ListIndex = tempPopupMenu.ListCount - 1

Else
tempPopupMenu.ListIndex = -1 'not found and don’t add, so disable choice
End If
End Sub[/code]