Reputation: 147
First of all Thanks for your Time! I hope you can help me =/
I have a MenuStrip where i want to add items dynamically.
What i want to do : If a Partent with exactly the same name already exists the Childs should be add to this parent instead creating a new Parent(MenuStripItem) with the same name.
My Code does currently checks if the parent already exists (which works fine) but the problem is i cant get the reference to this parent -> firstItem=var doesnt work -> cant convert ToolStripItem to ToolStripMenuItem... and changing the "firstItem" to ToolStripItem gives me an Error because i cant use "firstItem.DropDownItems.Add(withChild);" anymore to add a Child later on...
private void AddNewMenuStrips(string [,] NewMenuStripInfo)
{
ToolStripMenuItem firstItem;
bool alreadyexists = false;
string someItem = "Settings"; // the parent im looking for
var items = menuStrip2.Items.Find(someItem+"ToolStripMenuItem",false); //here it checks if parent already exists. Which Works but i cant get the reference of the parent to "firstItem"
foreach (var item in items)
{
MessageBox.Show("FOUND"+item.Name);
firstItem = var; // ERROR cant convert ToolStripItem to ToolStripMenuItem
alreadyexists=true;
}
if (alreadyexists == false) { firstItem = new ToolStripMenuItem(someItem); }
}
THANKS in advance!
Upvotes: 0
Views: 627
Reputation: 913
ToolStripMenuItem
is a class which represents top level menu items and derives (not directly) from ToolStripItem
.
Therefore to retrieve the parent menu item you can use a cast:
foreach (var item in parents)
{
MessageBox.Show("FOUND" + item.Name);
firstItem = item as ToolStripMenuItem;
alreadyexists = true;
// break;
}
Upvotes: 1