Search code examples
vb.netmenucontextmenuaddhandlertoolstripmenu

VB .net Context/ToolStripMenu at runtime


I have a ContextMenu like in this picture. Now i want to add where it says ".rtf" more MenuItems at runtime. I cant seem to make it...

Dim myContextMenu As ContextMenuStrip = Form1.mnuOptions
Dim myMenuItem As ToolStripMenuItem = myContextMenu.Items("SendTo")
Dim mySubMenuItem As New ToolStripMenuItem = myMenuItem.DropDownItems("File").SUBITEMS

This is how i imagine doing it, but it does not work of course because there is no ".subitems". What does it take to accomplish such an easy job?

And how can I set AddHandlers for each SubItem to a procedure?


Solution

  • Assuming that 'File' is the name of the menu item with that text, you can access it directly by it's name, and add the dropdown item to its collection:

    File.DropDownItems.Add("Pdf")
    

    One of the Add overloads allows you to indicate which event handler the click will fire.