XOJO v25r1.1 - Windows 11
I have fully changed my app to API2.
In my old code I had following code ,
Var mnu1 As MenuItem = mnuFrmMixMaster.Child(Instrument)
Var mnu2 As MenuItem = mnuFrmMixMaster.Child(midiDevice)
mnuFrmMixMaster.Remove(mnu2)
mnuFrmMixMaster.Remove(mnu1)
But now in API2 that are all become DesktopMenuItems.
Remove doesn’t exist anymore.
How can I make my old code correctly for API2
I tried : RemoveMenuAt(index As integer)
but how do I found the Index for mnu2 or mnu1 ?
Var mnu1 As DesktopMenuItem = mnuFrmMixMaster.Child(Instrument)
Var mnu2 As DesktopMenuItem = mnuFrmMixMaster.Child(midiDevice)
mnuFrmMixMaster.RemoveMenuAt(mnu2)
mnuFrmMixMaster.RemoveMenuAt(mnu1)
I tried : mnuFrmMixMaster.RemoveMenuAt(mnu2.Index) → but didn’t work
You’re not able to re-use menu items, so you should just be able to close those items after you get them by name. This is theory, I haven’t tried anything like that out since they fixed up closing / removing items on Windows.
Alternatively, you can loop through the children looking for the items. You can put this function on a global module to add a “RemoveMenu” function to DesktopMenuItem that will accept menu items as your design would like to do:
Public Sub RemoveMenu(extends mnuSource as DesktopMenuItem, mnuTarget as DesktopMenuItem)
for i as Integer = mnuSource.LastRowIndex downto 0
if mnuSource.MenuAt(i) = mnuTarget then
mnuSource.RemoveMenuAt(i)
exit for i
end
next
End Sub
If you just want to hide/show a MenuItem rather than remove it, you can use this:
Protected Sub doMenuBarRemoveMenuItem(myMenuBar As Object, MenuItemName As String, isMakeVisible As Boolean = False)
If myMenuBar = Nil Or MenuItemName = "" Then
Return
End If
#If TargetDesktop Then
If myMenuBar IsA DesktopMenuBar Then
Var tempMenuBar As DesktopMenuBar = DesktopMenuBar(myMenuBar)
For tempInt As Integer = 0 To tempMenuBar.Count - 1
Var tempMenuItem As DesktopMenuItem = tempMenuBar.MenuAt(tempInt)
Var MenuName As String = tempMenuItem.Name
Var RowMenuItem As DesktopMenuItem = tempMenuItem.Child(MenuItemName)
If RowMenuItem <> Nil Then
#If TargetWindows Then
If Not isMakeVisible Then 'Windows can only remove a MenuItem!
'RowMenuItem.Visible = isMakeVisible 'does nothing in Windows!
'RowMenuItem.Enabled = isMakeVisible 'does nothing in Windows!
'RowMenuItem.Close 'does nothing in Windows!
'tempMenuItem.Remove(RowMenuItem)
tempMenuItem.Visible = isMakeVisible
tempMenuItem.RemoveMenuAt(tempInt)
End If
#Else
RowMenuItem.Visible = isMakeVisible
'tempMenuItem.Remove(RowMenuItem)
'tempMenuItem.RemoveMenuAt(tempInt) 'doesn't seem to remove the menu in macOS!
#EndIf
Exit
End If
Next
End If
#EndIf
End Sub