How to trap option in a popupmenu and respond to it

I have the following popup menu. It doesn’t look like the IF statement is the right way to trap the selected option. The messagebox message is not displayed. Thanks for the help.

Var options() as String = Array("Word Frequency",_
"Word Bigrams", _
"Word Trigrams"
)
for each opt as String in options
  Me.AddRow(opt)
next

if PopupStatsMenu.SelectedRowValue = "Word Frequency" then
  MessageBox("The option selected is Word Frequency")

To avoid mishaps with typos, languages, and other string-related no-nos, you should avoid SelectedRowValue.

In the Changed event, check the SelectedRowIndex and make your decisions appropriately.

The thing I think you’re missing is that the PopupMenu setup code and the selection changed handler are different events.

2 Likes

The population of the popup menu and it’s action when selected are separate in Xojo.

First you can build the popup menu. You can either do this in the IDE by clicking on the control and settings properties of it, or in code as you have above:

You can then add events to your popup menu. For example you could put this code in your Opening event:

Var options() as String = Array("Word Frequency",_
"Word Bigrams", _
"Word Trigrams"
)
Me.RemoveAllRows()
for each opt as String in options
  Me.AddRow(opt)
next

You can then add a SelectionChanged event which will activate when the user selects something from the popup menu.

if PopupStatsMenu.SelectedRowValue = "Word Frequency" then
  MessageBox("The option selected is Word Frequency")
end if

Tim Parnell:

Yes, that’s it – the problem was I put the code to handle the option right after the code for the popup setup. Thank you

He is also right about using PopupStatsMenu.SelectedRowIndex rather than SelectedRowValue. You can have code like:

select case PopupStatsMenu.SelectedRowIndex
   case 0: ' Word Frequency
      ' Do Frequency
   case 1: ' Word Bigrams
      ' Do Bigrams
   case 2: ' Word Trigrams
      ' Do Trigrams
end select
1 Like