Programmatically add Sub Menus to a ContextMenuStrip

J

JB

Hi All,

I've always so far designed menus at design time and never had a
problem.
Today I want to create menus programmatically and add Sub Menus to a
ContextMenuStrip.
How can I programmatically reproduce what I do at design time by
setting the DropDown property of a Menu Item to the Sub Menu I wat to
attach to it?

The DropDown property doesn't exist on a ToolStripItem and I've tried
to play with the Owner property without any luck.

Any help or pointer would be greatly appreciated.
Thanks
JB
 
H

Herfried K. Wagner [MVP]

JB said:
I've always so far designed menus at design time and never had a
problem.
Today I want to create menus programmatically and add Sub Menus to a
ContextMenuStrip.
How can I programmatically reproduce what I do at design time by
setting the DropDown property of a Menu Item to the Sub Menu I wat to
attach to it?

The DropDown property doesn't exist on a ToolStripItem and I've tried
to play with the Owner property without any luck.

Just check the button on top of the solution explorer to show hidden files.
Then navigate to the "<form name>.Designer.vb" file of a form on which you
already added the toolstrip. It will contain the source code generated by
the designer to set up the menu. This should give you an idea on how to
create the menus.
 
J

JB

Just check the button on top of the solution explorer to show hidden files.
Then navigate to the "<form name>.Designer.vb" file of a form on which you
already added the toolstrip.  It will contain the source code generatedby
the designer to set up the menu.  This should give you an idea on how to
create the menus.

Thanks very much for that tip Herfried. I found the trick. The
designer uses a ToolStripMenuItem class and not a ToolStripItem as
returned by the ToolStripItemCollection.Add function.
 

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