Dynamic Menu Help please

Hello,

I have this code in a TextField.MouseDown Event…

[code] // Make the base item, this is what will be doing the popping up
dim base as new MenuItem
dim mItem1, mItem2 as MenuItem
dim smnu0 as new MenuItem

// Add a menu
mItem1 = New MenuItem
mItem1.Text = “Proximal 1/3”
mItem1.Tag = “Proximal 1/3”
base.Append(mItem1)

// Add some sub-menus
smnu0 = New MenuItem
smnu0.Text= “+ - mildly increased”
smnu0.Tag = “+”
mItem1.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “++ - moderately increased”
smnu0.Tag = “++”
mItem1.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “+++ - markedly increased”
smnu0.Tag = “+++”
mItem1.Append(smnu0)

// Add another menu
mItem2 = New MenuItem
mItem2.Text = “Proximal 2/3”
mItem2.Tag = “Proximal 2/3”
base.Append(mItem2)

// Add another set of sub-menus
smnu0 = New MenuItem
smnu0.Text= “+ - mildly increased”
smnu0.Tag = “+”
mItem2.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “++ - moderately increased”
smnu0.Tag = “++”
mItem2.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “+++ - markedly increased”
smnu0.Tag = “+++”
mItem2.Append(smnu0)

dim results as MenuItem
results = Base.PopUp
if results <> nil then
msgbox mItem1.Tag + " " + results.Tag + ""
end if[/code]

I would like to code in such a way that if I click
“Proximal 1/3”/"+ - mildly increased" the message box displays Proximal 1/3 +

and if I click
“Proximal 2/3”/"+ - ++ - moderately increased" the message box displays Proximal 2/3 ++

I know that
if results <> nil then
msgbox mItem1.Tag + " " + results.Tag + ""
end if

is coded for mItem1.Tag but how can I code it for whichever menu is coded?

Thanks.

Any other modifications is also appreciated.

Lennox

you have to hunt down which parent menu item the “results” selected belongs to

couple nested loops should do it
something like (this IS forum code I’ve not tested it but it should be close)

dim parent as menuitem
for i as integer = 0 to base.Count
  for j as integer = 0 to base.item(i).Count
         if base.item(i).item(j) is results then
             parent = base.item(i)
             exit for i
          end if
    next
next

now you have “parent” as results parent menu item and can get the tags texts etc

Thanks Norman,

I’ve been trying the code with several modifications and it only picks up the first menuitem - proximal 1/3.

I’ve modified it to remove all ambiguities and menuitem with the same name, so I now have this…

[code] // Make the base item, this is what will be doing the popping up
dim base as new MenuItem
dim mItem as MenuItem
dim smnu0 as new MenuItem

// Add a menu
mItem = New MenuItem
mItem.Text = “Proximal 1/3”
mItem.Tag = “Proximal 1/3”
base.Append(mItem)

// Add some sub-menus
smnu0 = New MenuItem
smnu0.Text= “+ - mildly increased”
smnu0.Tag = “+”
mItem.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “++ - moderately increased”
smnu0.Tag = “++”
mItem.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “+++ - markedly increased”
smnu0.Tag = “+++”
mItem.Append(smnu0)

// Add a menu
mItem = New MenuItem
mItem.Text = “Distal 2/3”
mItem.Tag = “Distal 2/3”
base.Append(mItem)

// Add some sub-menus
smnu0 = New MenuItem
smnu0.Text= “+! - mildly increased!”
smnu0.Tag = “+!”
mItem.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “++! - moderately increased!”
smnu0.Tag = “++!”
mItem.Append(smnu0)

smnu0 = New MenuItem
smnu0.Text= “+++! - markedly increased!”
smnu0.Tag = “+++!”
mItem.Append(smnu0)

dim results as MenuItem
results = Base.PopUp
if results <> nil then
msgbox results.text

dim parent as menuitem
for i as integer = 0 to base.Count
  for j as integer = 0 to base.item(i).Count
    if base.item(i).item(j) is results then
      parent = base.item(i)
      msgbox "Parent found " + parent.tag
      exit for i
    end if
  next
next

msgbox parent.tag + " *" + results.Tag + "*"

end if[/code]

It still does not pick up the second menuitem - Distal 2/3

How can I fix that?

Thanks.

Lennox

By NOT just copying & pasting my code
I did say it might not be quite right
It has issues and you would find that by debugging it and looking in the language reference to see that the loops should be to "count-1) not “count”
In fact you SHOULD get an out of bounds exception if you debug & have “break on exceptions” turned on (I almost never run without that being on)

Dim results As MenuItem
results = Base.PopUp
If results <> Nil Then
  MsgBox results.Text
  
  Dim parent As menuitem
  For i As Integer = 0 To base.Count-1
    For j As Integer = 0 To base.item(i).Count-1
      If base.item(i).item(j) Is results Then
        parent = base.item(i)
        MsgBox "Parent found " + parent.tag
        Exit For i
      End If
    Next
  Next
  
  MsgBox parent.tag + " *" + results.Tag + "*"
End If

Thanks Norman,

sorry about that, I now have “break on exceptions” turned on.

Thanks again.

I haven’t checked “This answers my question” as yet, I will in a short while.

Lennox

Thanks Norman,

All seems to be well now.

Thanks again.

Lennox