Dynamic items to ContextMenuStrip and Text from sub-items?

R

retrocoder

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.
 
S

Steffen

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
 
S

sorrowman

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.

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 :)
 
S

Steffen

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
 

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