Try this code..
Made some small modification on
OrignalGriff Solution..
Since
ToolStripItems
are not derived from
Control
Class, they need to be handled separately as below. this is will be applicable for other controls which is not derived from
Control Baseclass
so has to be modified as per the Base type.
private void Form2_Load(object sender, EventArgs e)
{
List<string> names = new List<string>();
GetNames(this, names);
var output = names;
}
private void GetNames(Control c, List<string> names)
{
foreach (Control sub in c.Controls)
{
names.Add(sub.Name);
if (sub is ToolStrip)
names.AddRange((sub as ToolStrip).Items.OfType<ToolStripItem>().Select(k => k.Name));
GetNames(sub, names);
}
}
Alternative Method:
var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
path = path.Substring(0, path.IndexOf("bin")) + "\\" + this.Name + ".Designer.cs";
var allines = File.ReadAllLines(path);
int index = allines.Select(k => k.Trim()).ToList().IndexOf("#endregion");
var names = allines.Select(k => k.Trim()).Skip(index + 1).Select(k =>
{
var temp = k.Split(' ');
if (temp.Length == 3)
return temp[2].Replace(";", "");
return k;
}).Where(k => k != "" && k != "}").ToList();
var output = names;