How to add dynamically new item WITH subitems to ContextMenuStrip?

I already have designer edited ContextMenuStrip on the form but I need
to add some items with sub-items to it dynamically on runtime. How to
do that?

I can add normal items with

myMenu.Items.Add("My new item");

But I have no idea how to add Item with sub-items.


Other question is, how can I get "Text" from sub-items and those new
dynamically created sub-items? I know I can do

Point p = myMenu.PointToClient(new Point(Form.MousePosition.X,
Form.MousePosition.Y));
ToolStripItem pItem = myMenu.GetItemAt(p.X, p.Y);
this.Text = pItem.Text;

But it only give text from main items, not from sub-items.

Any ideas are apreciated, thank you.

Re: Dynamic items to ContextMenuStrip and Text from sub-items? by Steffen

Steffen
Mon Jun 12 13:16:00 CDT 2006

> How to add dynamically new item WITH subitems to ContextMenuStrip?
>
> I already have designer edited ContextMenuStrip on the form but I need
> to add some items with sub-items to it dynamically on runtime. How to
> do that?
>
> I can add normal items with
>
> myMenu.Items.Add("My new item");
>
> But I have no idea how to add Item with sub-items.


Use code like this:

ToolStripMenuItem item = new ToolStripMenuItem("Item Text");
item.DropDownItems.Add("Sub Item Text");
myMenu.Items.Add(item);

Instead of 'item.DropDownItems.Add("Sub Item Text")' you can also use
'item.DropDownItems.Add(subitem)' where 'subitem' is another
ToolStripMenuItem object.


> Other question is, how can I get "Text" from sub-items and those new
> dynamically created sub-items? I know I can do


Just cast an item from your ContextMenuStrip to a ToolStripMenuItem
object and use the 'DropDownItems' property of this object.


I hope this helps you,
Steffen


Re: Dynamic items to ContextMenuStrip and Text from sub-items? by sorrowman

sorrowman
Mon Jun 12 14:47:10 CDT 2006

> Use code like this:
>
> ToolStripMenuItem item = new ToolStripMenuItem("Item Text");
> item.DropDownItems.Add("Sub Item Text");
> myMenu.Items.Add(item);
>
> Instead of 'item.DropDownItems.Add("Sub Item Text")' you can also use
> 'item.DropDownItems.Add(subitem)' where 'subitem' is another
> ToolStripMenuItem object.

Yes, worked like a charm. Thank you :) Got it working now as I was
hoping.


> > Other question is, how can I get "Text" from sub-items and those new
> > dynamically created sub-items? I know I can do
>
>
> Just cast an item from your ContextMenuStrip to a ToolStripMenuItem
> object and use the 'DropDownItems' property of this object.

Didn't yet get this part working but I only tested it with
myMenu.ItemClick event sofar. I get ItemClicked event for "main" items
in menu, but not for subitems for some reasons, probably something
simple (again) I am missing, so better take nap here and attack problem
again when "fresh" and can hopefully think, heh.



> I hope this helps you,
> Steffen

Anyway, thank you for the help :)


Re: Dynamic items to ContextMenuStrip and Text from sub-items? by Steffen

Steffen
Tue Jun 13 03:40:54 CDT 2006

> Didn't yet get this part working but I only tested it with
> myMenu.ItemClick event sofar. I get ItemClicked event for "main" items
> in menu, but not for subitems for some reasons, probably something
> simple (again) I am missing, so better take nap here and attack problem
> again when "fresh" and can hopefully think, heh.

You can write a detailled description of your problem if you need more
help (I actually don't know what you are planning with ItemClick).

Steffen