473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

MenuItem doesn't get used twice.

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
Nov 20 '05 #1
6 1308
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
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

Nov 20 '05 #2
> 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.

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.676 / Virus Database: 438 - Release Date: 03/05/2004
Nov 20 '05 #3
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
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

Nov 20 '05 #4
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
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
Nov 20 '05 #5
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

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> schrieb
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. ;-)

LOL


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

Nov 20 '05 #6
"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:eV**************@tk2msftngp13.phx.gbl...
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)
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.676 / Virus Database: 438 - Release Date: 03/05/2004
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marcel | last post by:
Hi, the application I am writing has a MainMenu and a DataGrid (UltraGrid). Now I have to edit the text of each cell in this grid. So far so good. All works fine in editmode but if I press a...
4
by: Jesper | last post by:
Hi, When creating a popup menu I find it rather difficult to provide the eventhandler for the MenuItem with enough information. My actual problem is that I create a popup menu with a list of...
6
by: Claus Holm | last post by:
I'm trying to enable a menuitem in the parent form from a mdichild. Rather than making the menuitems public, I'd go for a public method in the parent form to do the change, but when I call the...
4
by: Claire | last post by:
Sorry Ive added this twice (sortof) but if I'd added an addendum to the first one then this would probably have been ignored. This problem affects a ContextMenu attached to a NotifyIcon object. I...
1
by: Eric Sabine | last post by:
I have a function that returns a menuitem() array. This array is used elsewhere within the application, but most importantly in the system tran in a notify icon. All works great except that for...
15
by: Jeff Mason | last post by:
I have an application where I need to set certain menu items invisible based on a user privilege. We did a sinmlar thing in VB6 and used the menu item's tag property to assign an identifier to...
4
by: Jeff Mason | last post by:
I have an application where I need to set certain menu items invisible based on a user privilege. We did a sinmlar thing in VB6 and used the menu item's tag property to assign an identifier to...
2
by: Agnes | last post by:
I know how to get the Menuitem's TEXT . BUT I need to get the names , Does anyone know how to do ?? Thanks I try That " For Each FileMenuItem As MenuItem In Me.Menu.menuitem" before
1
by: Carl Ganz | last post by:
I'm trying to build a MenuItem object in a web service and return it to the proxy. Something like this: public MenuItem GetReportMenu() { MenuItem oMenuItem = null; oMenuItem = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.