1

I've got a Windows Forms menu bar, with one menu item. Below that is 1 Sub Item.

Let's say File → Open.

I'm trying to get a reference to the sub menu, but it is giving me a hard time.

ToolStripItem main = menuMain.Items["File"]; //This is fine.
ToolStripMenuItem sub =  main.DropDownItems(0); 
//Toolstrip item does not contain an extension for DropDownItems ??

How can I get a reference to the sub menu item?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Cameron Castillo
  • 2,712
  • 10
  • 47
  • 77

2 Answers2

1

Try this:

ToolStripDropDownButton main = (ToolStripDropDownButton) toolStrip1.Items["File"];
ToolStripMenuItem sub = (ToolStripMenuItem) main.DropDownItems["FileOpen"];

You need to cast the items to the right class. Please note that "File" and "FileOpen" are the names of the object.

nvr
  • 113
  • 7
0

use:

var main = (ToolStripDropDownItem) menuMain.Items["File"];
var sub = main.DropDownItems[0]

DropDownItems property blongs to ToolStripDropDownItem which is the base class for:

  • ToolStripMenuItem
  • ToolStripDropDownButton
  • ToolStripSplitButton
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398