Exclude element from foreach index (MenuStripItem) HELP!
Hello!
I have a foreach loop. How can I exclude one of the elements from being indexed: I have a foreach loop setting a property for each of the items in a ToolStripMenuItem category, but one of the items is a ToolStripSeperator, and I would like to exclude it so the foreach loop would set the property for all of the items EXCEPT the toolstripseperator. How can I do that?
Matt
check to see the current control type you are on.
can you post some code please?
foreach
(ToolStripMenuItem item in
favoritesToolStripMenuItem.DropDownItems)item.ToolTipText =
"Site";Then I get an excpetion with something related to the ToolStripSeperator
Write it something like this
foreach (ToolStripItem item in favoritesToolStripMenuItem.DropDownItems)
{
ToolStripMenuItem menuItem = item as ToolStripMenuItem;
if (menuItem != null)
{
// ... code here ...
}
}
Although this will work
ToolStripMenuItem menuItem = item as ToolStripMenuItem;
I can't believe it's the best way. Won't it (internally) generate exceptions, check type info and all sorts of extra overhead.
Just check the type info first manually and you bypass all the possible errors and make the loop (possibly lots) faster.
foreach (ToolStripItem item in favoritesToolStripMenuItem.DropDownItems)
{
if(item.GetType() == typeof(ToolStripMenuItem))
{
item.ToolTipText = "Site";
}
}
Neil
No, it will not throw an exception (that's the whole point of using the as operator) and it will not add any extra overhead compared to calling GetType for example. As a bonus, it will work for classes derived from ToolStripMenuItem too, whereas your solution does not.
Maybe not an exception then, but I bet there is a whole slew of underlying code that traverses through the type information tree recursively, looking for the type. Which is a lot slower than just calling a virtual function.
I take your point about GetType not supporting derived classes though.
Try this code:
private void SetMenuItems(ToolStripMenuItem MyMenuItem)
{
foreach (object mni in MyMenuItem.DropDownItems)
{
if (mni.GetType() == typeof(ToolStripMenuItem))
{
if (((ToolStripMenuItem)mni).DropDownItems.Count > 0) SetMenuItems(((ToolStripMenuItem)mni));
((ToolStripMenuItem)mni).ToolTipText = "Site";
}
}
}
You just have to call this method for the parent menu item.
slk at 2007-10-8 >

Hey guys!
Thanks for the answers: One more thing: How can I do this:
foreach
(ToolStripItem item in
favoritesToolStripMenuItem.DropDownItems){
if
(item.GetType() == typeof
(ToolStripMenuItem)){
if
(item.ToolTipText == "1")favoritesToolStripMenuItem.DropDownItems.Remove(item);
}
}
It gives me an exception - An unhandled exception of type 'System.InvalidOperationException' occurred in mscorlib.dll
Additional information: Collection was modified; enumeration operation may not execute.
Matt
yes thats right.
you are adding an items into the collection but you are going through the collection at the same time, modifying the indexers and so on - not acceptable.
you should keep a list of items to remove in an array for example, then outside the loop, go through the array and remove the items
Can you post some simple code here, relating to what I want to do?
Matt
ArrayList theItemsToRemove = new ArrayList();
foreach (ToolStripItem item in favoritesToolStripMenuItem.DropDownItems)
{
if (item.GetType() == typeof(ToolStripMenuItem))
{
if (item.ToolTipText == "1")
{
theItemsToRemove.Add(item); //favoritesToolStripMenuItem.DropDownItems.Remove(item);
}
}
foreach (ToolStripItem currentItem in theItemsToRemove)
{
this.favoritesToolStripMenuItem.DropDownItems.Remove(currentItem);
}
does this work for you?