You’re not doing anything wrong with Menu.AddItem. The problem is how the existing menu is being accessed.
MainMenuBar is a generated module that creates menu-bar instances; it is not the menu bar currently attached to Window1. Its named menus are therefore not shared members such as MainMenuBar.openrecentitem.
Pass the window’s actual MenuBar instance to FillMenuRecent:
Event Opening()
Var recent As New clsRecent
If Not recent.FillMenuRecent(Self.MenuBar) Then
# Something went wrong
End If
End Event
Then find the submenu by its designer name:
Public Function FillMenuRecent(menuBar As MenuBar) As Boolean
Me.FillArrayRecent()
Var openRecent As Menu = menuBar.FindMenu("openrecentitem")
If openRecent = Nothing Then
Return False
End If
For i As Integer = 0 To arrFile.Count - 1
Var joined As String = arrFile[i] + "|" + arrPath[i] + "|" + arrFull[i]
Var menuText As String = arrFile[i] + " - " + Chr(128193) + " " + arrPath[i]
Var recentItem As New MenuItem(menuText)
recentItem.Name = "Recent_" + i.ToString("D2")
recentItem.Tag = joined
openRecent.AddItem(recentItem)
Next
Return True
End Function
FindMenu() searches the complete menu tree, including submenus.
Also, File is the displayed caption; the designer name of that menu is fileMenu. However, MainMenuBar.fileMenu would still not work because it is not a shared member.
Studio should have reported this invalid module-member access as a compile-time error rather than allowing it to become a runtime error. The runtime message also incorrectly calls MainMenuBar a class when it is a module. Those diagnostics need correcting - I'm addressing that now.