Dynamically Adding Menu Items

J

jack

Hello,

I need to dynamically add menu items to an existing menu on an MDI form. In
the form load, when I create the menu items then add it to the menu control
using the Add method, the entire menu dissappears. Any one know why?

Below is the code I'm using to create the menu items and append them to the
existing menu:

Dim aDatasheets As ArrayList = gOSSystemFile.Datasheets
Dim oDatasheet As OSSystem.OSDatasheet
Dim mnuDatasheet As MenuItem

' existing top level menu item
mnuDatasheet = Me.mnuDatasheets

' add a submenu item for each datasheet in our set
For Each oDatasheet In aDatasheets
Dim mnuTemp As New MenuItem
mnuTemp.Text = oDatasheet.Name
mnuDatasheet.MenuItems.Add(mnuTemp)
Next


Thanks in advance.
Jack
 
M

Mike McIntyre [MVP]

Jack,

I created a class to define objects that could be used as sources for new menus:

Public Class MenuItemSource
' Name field.
Private _Name As String

' Name property.
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property

Public Sub New(ByVal menuName As String)
Me.Name = menuName
End Sub
End Class

Next, I added this code to a MDI form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim menuItemSources As New ArrayList()
menuItemSources.Add(New MenuItemSource("New Menu1"))
menuItemSources.Add(New MenuItemSource("New Menu2"))
Dim typeMenuItemSource As MenuItemSource
For Each typeMenuItemSource In menuItemSources
Dim newMenuItem As New MenuItem()
newMenuItem.Text = typeMenuItemSource.Name
Me.FileMenuItem.MenuItems.Add(newMenuItem)
Next
End Sub
It all worked just fine.

Are you sure about the data being used in your example?


--
Mike

Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com
 
J

jack

It took me a while, but I figured out what the problem was.

The app in question is currently being converted from VB6 app to .NET.
Because VB6 did not support docking, the app had a picture box on the MDI
form into which another form is loaded, thus acting as if it is docked. As
it turns out, somehow the issue is related to the picture box and loading a
form into it. Once I dumped the picture box, the menu issue was resolved.
Part of my task to fully convert this app to .NET is to get rid of the
picture box. It just happened sooner than I planned.

Thanks!
Jack


Jack,

I created a class to define objects that could be used as sources for new
menus:

Public Class MenuItemSource
' Name field.
Private _Name As String

' Name property.
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property

Public Sub New(ByVal menuName As String)
Me.Name = menuName
End Sub
End Class

Next, I added this code to a MDI form:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim menuItemSources As New ArrayList()
menuItemSources.Add(New MenuItemSource("New Menu1"))
menuItemSources.Add(New MenuItemSource("New Menu2"))
Dim typeMenuItemSource As MenuItemSource
For Each typeMenuItemSource In menuItemSources
Dim newMenuItem As New MenuItem()
newMenuItem.Text = typeMenuItemSource.Name
Me.FileMenuItem.MenuItems.Add(newMenuItem)
Next
End Sub
It all worked just fine.
Are you sure about the data being used in your example?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top