Get a Windows Forms control by name in C#

user186249 picture user186249 · Oct 8, 2009 · Viewed 207.2k times · Source

I have a ToolStripMenuItem called myMenu. How can I access this like so:

/* Normally, I would do: */
this.myMenu... etc.

/* But how do I access it like this: */
String name = myMenu;
this.name...

This is because I am dynamically generating ToolStripMenuItems from an XML file and need to reference MenuItems by their dynamically generated names.

Answer

Adriaan Stander picture Adriaan Stander · Oct 8, 2009

Use the Control.ControlCollection.Find method.

Try this:

this.Controls.Find()