Finding the selected index in the Contextual menu?

OK, I think the discussion got confused. You had the Event Handler, but didn’t know how to access the selected item which, as I said just above, is the hitItem parameter. So something like:

if hitItem.Text = "Some Item" then
  ' Do something
end if

Or

if hitItem.Index = 0 then
  ' Do something
end if

Or

Select Case hitItem.Index
Case 0
  ' Do something
end select
1 Like

Read the documentation, there is a very small example there.

Have you looked at the example called “Creating a Contextual Menu.xojo_binary_project” ?
Look in the Xojo “Examples” folder.

Yes! This did it, thanks so much all. :slight_smile:

Scratch that. I just checked and there’s still a bug here. MenuItem.Index returns obscene numbers that don’t actually match the index in the menu. You’ll have to use either the text or the Tag property. Example of creating items with the Tag property:

Function ConstructContextualMenu(base as MenuItem, x as Integer, y as Integer) Handles ConstructContextualMenu as Boolean
  var itemOne as new MenuItem( "One" )
  itemOne.Tag = 0
  base.AddMenu( itemOne )
  
  var itemTwo as new MenuItem( "Two" )
  itemTwo.Tag = 1
  base.AddMenu( itemTwo )
  
  Return True
End Function

Example of responding to said items:

Function ContextualMenuAction(hitItem as MenuItem) Handles ContextualMenuAction as Boolean
  var itemIndex as Integer = hitItem.Tag.IntegerValue
  select case itemIndex
  case 0
    ' Item One
  case 1
    ' Item Two
  end select
End Function

Happy to help. Don’t forget to mark a solution to your topic(s) so others who may experience the same issue in the future can quickly find what helped you.

1 Like

Thanks again for taking the time. I used this code:
Screenshot 2022-08-10 at 10.37.33

1 Like

For those interested, I’ve opened a case for DesktopMenuItem.Index being incorrect.

1 Like

LiveText is our friend:


Function ContextualMenuAction(hitItem as MenuItem) Handles ContextualMenuAction as Boolean
  // Create a new Character with the selected text as character name.
  Var CreateCharacter As String = "Create New Character from Selection"
  Var CreateLocation  As String = "Create New Location from Selection"
  Var txt As String = TextWritingAreaStep1.SelectedText
  
  // 1. Add selected text to a variable.
  // If no text was selected..
  If txt.IsEmpty Then
    MessageBox("Nothing was selected.")
    Return False
  End If
  
  // Create a new Character from selection
  If hitltem.Text = CreateCharacter Then
    ListBoxCharacters.AddRow(txt)
    ListBoxCharacters1.AddRow(txt)
    MessageBox(txt + " has been added to the character list.")
    Return True
  End If
  
  // Create a new Character from selection
  If hitltem.Text = CreateLocation Then
    MessageBox(txt + " has been added to the location list.")
    Return True
  End If
End Function

If this code is in the contextmenuaction event, its probably too late to be testing whether any text was selected, I think.

This goes in the ConstructContextualMenu event:

  If me.SelectedText.IsEmpty Then
    MessageBox("Nothing was selected.")
else

//construct the menu
  End If

and then the action event becomes


select case hititem.text
case "Create New Character from Selection"
  // Create a new Character from selection

    ListBoxCharacters.AddRow(txt)
    ListBoxCharacters1.AddRow(txt)
    MessageBox(txt + " has been added to the character list.")
case  "Create New Location from Selection"

    MessageBox(txt + " has been added to the location list.")
  //although you realise you didnt DO anything here??
case else
end select

End Function