473,387 Members | 1,529 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,387 software developers and data experts.

ContextMenu: Dynamically adding sub menu items

When I try to dynamically add a second sub menu item to this ContextMenu
item, I get an error 'Specified argument was out of the range of valid
values'.

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Dim mShowMenuItem As MenuItem
mShowMenuItem = DirectCast(sender, MenuItem)
mShowMenuItem.MenuItems.Clear()

' The first sub menu item gets added withouot any errors:
Dim mSubMenuItem As New MenuItem()
mSubMenuItem.Text = "Sub Item #1"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mShowMenuItem.MenuItems.Add(mSubMenuItem)

' This second sub menu item causes a problem:
mSubMenuItem.Text = "Sub Item #2"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
'error occurs here:
mShowMenuItem.MenuItems.Add(mSubMenuItem)

End Sub

What am I doing wrong?
Thanks


Nov 20 '05 #1
10 21205
"tmaster" <le*******@travelmasterusa.com> schrieb
When I try to dynamically add a second sub menu item to this
ContextMenu item, I get an error 'Specified argument was out of the
range of valid values'.

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object,
_
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Dim mShowMenuItem As MenuItem
mShowMenuItem = DirectCast(sender, MenuItem)
mShowMenuItem.MenuItems.Clear()

' The first sub menu item gets added withouot any errors:
Dim mSubMenuItem As New MenuItem()
mSubMenuItem.Text = "Sub Item #1"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mShowMenuItem.MenuItems.Add(mSubMenuItem)

' This second sub menu item causes a problem:
mSubMenuItem.Text = "Sub Item #2"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
'error occurs here:
mShowMenuItem.MenuItems.Add(mSubMenuItem)

End Sub

What am I doing wrong?
You create only one menu item but you add it twice. This is not possible.
You have to create two menu items:

'... ' This second sub menu item causes a problem:

mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #2"

'...

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Thanks for your help. I am still having a problem with adding the second
sub menu item. If I add only one item (either one), it works fine. When I
add the 'Add second sub menu item' paragraph, then neither of the sub menu
items appear. (However, I do see the 'arrow' indicating that there are sub
items). Here is the code:

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Dim mShowMenuItem As MenuItem
mShowMenuItem = DirectCast(sender, MenuItem)
mShowMenuItem.MenuItems.Clear()

Dim mSubMenuItem As MenuItem

' Add first sub menu item. Works fine if I don't add a second sub
menu item.
mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #1"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mShowMenuItem.MenuItems.Add(mSubMenuItem)

' Add second sub menu item. Now, cannot see either sub menu items.
(?)
mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #2"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mShowMenuItem.MenuItems.Add(mSubMenuItem)

End Sub

Thanks again.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
"tmaster" <le*******@travelmasterusa.com> schrieb
When I try to dynamically add a second sub menu item to this
ContextMenu item, I get an error 'Specified argument was out of the
range of valid values'.

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object,
_
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Dim mShowMenuItem As MenuItem
mShowMenuItem = DirectCast(sender, MenuItem)
mShowMenuItem.MenuItems.Clear()

' The first sub menu item gets added withouot any errors:
Dim mSubMenuItem As New MenuItem()
mSubMenuItem.Text = "Sub Item #1"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mShowMenuItem.MenuItems.Add(mSubMenuItem)

' This second sub menu item causes a problem:
mSubMenuItem.Text = "Sub Item #2"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
'error occurs here:
mShowMenuItem.MenuItems.Add(mSubMenuItem)

End Sub

What am I doing wrong?


You create only one menu item but you add it twice. This is not possible.
You have to create two menu items:

'...
' This second sub menu item causes a problem:

mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #2"

'...

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
"tmaster" <le*******@travelmasterusa.com> schrieb
Thanks for your help. I am still having a problem with adding the
second sub menu item. If I add only one item (either one), it works
fine. When I add the 'Add second sub menu item' paragraph, then
neither of the sub menu items appear. (However, I do see the 'arrow'
indicating that there are sub items). Here is the code:

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object,
_
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Handle the Popup event instead.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
I get the same results.
The 'Select' event will show one sub menu item if only one if Added. It
shows neither when two are Added.
The 'Popup' event shows niether one nor two sub options, when they are
Added.
The 'Click' event shows niether one nor two sub options, when they are
Added.
Is there something else I could be doing incorrectly?

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ur**************@TK2MSFTNGP11.phx.gbl...
"tmaster" <le*******@travelmasterusa.com> schrieb
Thanks for your help. I am still having a problem with adding the
second sub menu item. If I add only one item (either one), it works
fine. When I add the 'Add second sub menu item' paragraph, then
neither of the sub menu items appear. (However, I do see the 'arrow'
indicating that there are sub items). Here is the code:

Private Sub mnuTopics_Show_Select(ByVal sender As System.Object,
_
ByVal e As System.EventArgs) _
Handles mnuTopics_Show.Select

Handle the Popup event instead.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #5
"tmaster" <le*******@travelmasterusa.com> schrieb
I get the same results.
The 'Select' event will show one sub menu item if only one if Added.
It shows neither when two are Added.
The 'Popup' event shows niether one nor two sub options, when they
are Added.
The 'Click' event shows niether one nor two sub options, when they
are Added.
Is there something else I could be doing incorrectly?

Sorry, my mistake! Handle the popup event of the *parent* of mnuTopics_Show.
Reason: mnuTopics_Show is shown when it's parent is opened. There you have
to fill the sub menu items of mnuTopics because the arrow is painted
depending on the count of visible sub menu items.
But, you have to change the code because you don't want to add the sub menu
items to the parent but to mnuTopics_Show itself. I tested it and it works.

If mnuTopics has no parent, it should work when handling it's popup event.
Also tested this time.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
Thanks again for your help. I think I'm getting close. As you suggest, I
handle the popup event of the *parent* of mnuTopics_Show, and, find the
'Show Type' menu option, then add the sub menu options. All works fine the
first time I access the sub menu items. But, for some reason, the sub menu
items do not appears on subsequent accesses. Here's the code:

Private Sub mnuTopics_Popup( _
ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles mnuTopics.Popup

' All of the code in this sub is dedicated to populating the
sub-menu-items
' of the 'Show Type' menu option:

' get the parent of 'Show Type' menu item:
Dim mMenu As Menu
mMenu = DirectCast(sender, Menu)

' Cycle through all of its options to find the 'Show Type' sub menu
option:
Dim mItem As MenuItem
For Each mItem In mMenu.MenuItems
If mItem.Text.ToString = "Show Type" Then
mItem.MenuItems.Clear()

Dim mSubMenuItem As MenuItem

' Add first sub menu item.
mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #1"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mItem.MenuItems.Add(mSubMenuItem)

' Add second sub menu item.
mSubMenuItem = New MenuItem()
mSubMenuItem.Text = "Sub Item #2"
mSubMenuItem.Enabled = True
mSubMenuItem.Visible = True
AddHandler mSubMenuItem.Click, AddressOf MenuClickhandler
mItem.MenuItems.Add(mSubMenuItem)

End If
Next

End Sub

"Armin Zingler" <az*******@freenet.de> wrote in message
news:ea*************@tk2msftngp13.phx.gbl...
"tmaster" <le*******@travelmasterusa.com> schrieb
I get the same results.
The 'Select' event will show one sub menu item if only one if Added.
It shows neither when two are Added.
The 'Popup' event shows niether one nor two sub options, when they
are Added.
The 'Click' event shows niether one nor two sub options, when they
are Added.
Is there something else I could be doing incorrectly?

Sorry, my mistake! Handle the popup event of the *parent* of

mnuTopics_Show. Reason: mnuTopics_Show is shown when it's parent is opened. There you have
to fill the sub menu items of mnuTopics because the arrow is painted
depending on the count of visible sub menu items.
But, you have to change the code because you don't want to add the sub menu items to the parent but to mnuTopics_Show itself. I tested it and it works.
If mnuTopics has no parent, it should work when handling it's popup event.
Also tested this time.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #7
"tmaster" <le*******@travelmasterusa.com> schrieb
Thanks again for your help. I think I'm getting close. As you
suggest, I handle the popup event of the *parent* of mnuTopics_Show,
and, find the 'Show Type' menu option, then add the sub menu options.
All works fine the first time I access the sub menu items. But, for
some reason, the sub menu items do not appears on subsequent
accesses. Here's the code:

I used your code but I can not reproduce the problem.

- Have you already tried to single-step through the procedure?

- Why do you search for "Show Type"? Don't you have a reference (e.g.
mnuTopicsShowType)?

- Short version of
If mItem.Text.ToString = "Show Type" Then
is
If mItem.Text = "Show Type" Then
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8
Regarding 'Why do you search for "Show Type"? Don't you have a reference
(e.g.mnuTopicsShowType)?'
I changed the search to a direct reference. Simplified things a bit. thanks.

Still had the problem. So, I moved the code to right before where the
ContextMenu is being invoked:
Me.mnuTopics.Show(Me.tvwTopics, New Point(e.X, e.Y))

which is in the Mouseup event of a treeview. Still had the same problem.

So I moved the code to my Initialization routine (before anything is
presented to the user). All works fine now. Problem must be related to my
trying to invoke the ContextMenu Show method from within the treeview's
mouseup event.

I really want the context sub menu items to be generated after the user
selects some node in a large treeview (based on what is currently in the
treeview). Do you have suggestions on when to lanuch the ContextMenu other
than using the MouseUp event of the TreeView?

Sorry about all of the newbie questions. I've been coding in VB6 for a good
while, but am just getting into VB.NET. By the way, I just realized that my
email address that shows for me (below) is incorrect. It should be
le*******@travelmasterusa.net.

Thanks again for all of your help.

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"tmaster" <le*******@travelmasterusa.com> schrieb
Thanks again for your help. I think I'm getting close. As you
suggest, I handle the popup event of the *parent* of mnuTopics_Show,
and, find the 'Show Type' menu option, then add the sub menu options.
All works fine the first time I access the sub menu items. But, for
some reason, the sub menu items do not appears on subsequent
accesses. Here's the code:

I used your code but I can not reproduce the problem.

- Have you already tried to single-step through the procedure?

- Why do you search for "Show Type"? Don't you have a reference (e.g.
mnuTopicsShowType)?

- Short version of
If mItem.Text.ToString = "Show Type" Then
is
If mItem.Text = "Show Type" Then
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
"tmaster" <le*******@travelmasterusa.net> schrieb
Sorry about all of the newbie questions. I've been coding in VB6 for
a good while, but am just getting into VB.NET. By the way, I just
realized that my email address that shows for me (below) is
incorrect. It should be le*******@travelmasterusa.net.

I'll sent you a project to this address. I hope it shows the difference to
your project because at current I don't have a solution for you (apart from
the one I sent *g*).
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10
I've seen this before, and the solution was simple when I eventually found
it.
The cause is that you are not assigning the ContextMenu to the Control from
which it is popping up. You are either using the ContextMenu.Show method,
to popup the menu with a Left Click, or are using the TrackPopupMenu API in
order to Align the Context Menu.

Solution:
1. Remove the parent menu item from the ContextMenu.
2. Add child menuitems to the parent menu item.
3. ReInsert Parent Menuitem in the ContextMenu.

To make your dynamicmenu creation faster creat all the MenuItems in a loop
and then use addrange to add them all at once. You will see a significant
improvement in speed.

PseudoCode:
\\\
Dim mi(ItemsToAdd.Count - 1) As MenuItem 'Zero Based
For i As Integer = 0 To ItemsToAdd.Count - 1
mi(i) = New MenuItem (ItemsToAdd(i).Text, AddressOf MenuItemClick)
Next
ContextMenu.MenuItems.Remove(ParentMenuItem)
ParentMenuItem.MenuItems.Clear
ParentMenuItem.MenuItems.AddRange(mi)
ContextMenu.MenuItems.Add(ParentMenuItemIndex, ParentMenuItem)
///
Nov 20 '05 #11

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

Similar topics

0
by: williams | last post by:
Hello, I need to develope an awt-based app, which requires a list menuitems are created based on current application model when the Menu to which the list of menuitems are attached is clicked....
52
by: DJ WIce | last post by:
Hi all, I did make a script/css thing to replace the contextmenu on the website with a new one: http://www.djwice.com/contextmenu.html It works nice in MSIE, but on Netscape (and probable...
0
by: Michael Evenson | last post by:
Is there any way to add a menu item to an existing drop down menu in Excel using the Visual Studio Tools for Microsoft Office 2003? Specifically, I want to add a menu item to the drop down list you...
2
by: jack | last post by:
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...
2
by: Ludwig | last post by:
My application has a main tool strip menu (File, Options, help,...). There are also a number of context menu's that popup. Some main menu items should also be available in some context menu's. ...
1
by: MJ | last post by:
I want to be able to add menu items at run time. I know how to add the actual item but can't work out how to add the event handler. Any help would be appreciated. MJ
0
by: Jared Bohlken | last post by:
I am using Visual Basic 2005. I have a program where I add items to a drop down menu on a toolbar during runtime. When I click on any one of those items, I need it to shell the program. How...
2
by: MCM | last post by:
I'm working on a plotting control. The plotting control will have a context menu with basic commands for "scaling", "zooming", etc. Is there a way that, from the parent form, I can add more...
6
by: Luqman | last post by:
How can I dynamically Add menuStrip and its menuItems through code using VB.Net 2005 ? Best Regards, Luqman
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.