MenuItem doesn't get used twice.

E

Eric Sabine

Basically, the following code creates a menuItem array and tries to use it
twice. In the following piece of code, only the line that appears second
gets used. The first becomes ignored presumably when the second is run.

Me.mnuFile.MenuItems.AddRange(myMenu)
Me.ContextMenu1.MenuItems.AddRange(myMenu)

Below I've included just enough code to see it as an example. Can someone
explain to me why this happens?

Eric



Public Class Form1

Inherits System.Windows.Forms.Form
Private myMenu() As MenuItem
Private components As System.ComponentModel.IContainer
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Public Sub New()
MyBase.New()
InitializeComponent()
Dim mic() As MenuItem
mic = New MenuItem(2) {}
mic(0) = New MenuItem("a")
mic(0).Index = 1
mic(1) = New MenuItem("b")
mic(1).Index = 2
mic(2) = New MenuItem("c")
mic(2).Index = 3
myMenu = mic
Me.mnuFile.MenuItems.AddRange(myMenu)
Me.ContextMenu1.MenuItems.AddRange(myMenu)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub


Private Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu
Me.ContextMenu1 = New System.Windows.Forms.ContextMenu
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.mnuFile = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.mnuFile})
Me.TextBox1.ContextMenu = Me.ContextMenu1
Me.TextBox1.Location = New System.Drawing.Point(56, 64)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
Me.mnuFile.Index = 0
Me.mnuFile.Text = "File"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.TextBox1)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub

End Class
 
A

Armin Zingler

Eric Sabine said:
Basically, the following code creates a menuItem array and tries to
use it twice. In the following piece of code, only the line that
appears second gets used. The first becomes ignored presumably when
the second is run.

Me.mnuFile.MenuItems.AddRange(myMenu)
Me.ContextMenu1.MenuItems.AddRange(myMenu)


You can not attach the same menuitems to different menus - just like the
same control can not be placed on two different Form instances.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
M

Mick Doherty

Basically, the following code creates a menuItem array and tries to use it
twice. In the following piece of code, only the line that appears second
gets used. The first becomes ignored presumably when the second is run.

Me.mnuFile.MenuItems.AddRange(myMenu)
Me.ContextMenu1.MenuItems.AddRange(myMenu)

Below I've included just enough code to see it as an example. Can someone
explain to me why this happens?

Eric

The first line is not ignored.
You add the myMenu Items to Me.MenuFile. Imediately after this this you add
the same Items to Me.ContextMenu1. There is only one array of the Items and
they cannot be in two places at once so before they can be added to
ContextMenu1 they are removed from mnuFile.
 
E

Eric Sabine

Thanks Armin and Mick.

Can you guys suggest on how I can use the same menuitem() on two menus.
Basically I have a function that returns the menuitem array which gets built
from a dataset. I want this menuitem array to be applied to actually 2
places. Two context menus and a mainmenu.

I don't want to have to build it twice from the function. I _could_, but
perhaps the control could be cloned or copied somehow. Do you have a
suggestion?

Eric
 
A

Armin Zingler

Eric Sabine said:
Thanks Armin and Mick.

Can you guys suggest on how I can use the same menuitem() on two
menus. Basically I have a function that returns the menuitem array
which gets built from a dataset. I want this menuitem array to be
applied to actually 2 places. Two context menus and a mainmenu.

Call the function twice to have the items created twice.
I don't want to have to build it twice from the function.

Too late, I've just suggested it. ;-)

I _could_,

I think you _must_ create the items twice.
but perhaps the control

Which control? A menu is not a Control, it's a Component.
could be cloned or copied somehow. Do you
have a suggestion?

Clone what? By creating the items twice you already have the same result as
cloning anything.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
E

Eric Sabine

OK. Thanks for clarifying component != control. It seemed to me that
creating it once and then cloning it would be the better programming route,
rather than create it twice. They two approaches do seem to be the same
thing, but I just figured clone/copy seemed (at least on the surface) to be
more efficient.

I sincerely appreciate your help.

Eric
 
M

Mick Doherty

Eric Sabine said:
Thanks Armin and Mick.

Can you guys suggest on how I can use the same menuitem() on two menus.
Basically I have a function that returns the menuitem array which gets built
from a dataset. I want this menuitem array to be applied to actually 2
places. Two context menus and a mainmenu.

I don't want to have to build it twice from the function. I _could_, but
perhaps the control could be cloned or copied somehow. Do you have a
suggestion?

Eric

Replace the line:
Me.ContextMenu1.MenuItems.AddRange(myMenu)
with
Me.ContextMenu1.MergeMenu(myMenu)
 

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