.NET 2.0 C#
Hi,
I have some code that tries to remove a MenuItem from a MenuStrip using :
theFormName.menuStrip1.Items.RemoveByKey("MenuX");
I can debug the code, and I know have a valid pointer to the Form : I know I
have a valid pointer to the MenuStrip control on that Form : and I know
there is a MenuItem in that MenuStrip on that Form which has the text
"MenuX" :
And no error is thrown when the line of code is executed, but the MenuItem
is not removed. MenuItems have no separate "key" property, so I have to
assume IntelliSense is informing me correctly that "key" is a string when I
hover over "RemoveByKey."
I am iterating a number of forms, and trying to remove the same menu item
from all forms, but this ain't working. I've tried refreshing the Form after
the RemoveByKey call with no results. Examining the MenuItem collection
after the call and looking at its InnerList property using the Property
Inspector shows it's still there.
This is a case where it would be very convenient to remove the MenuItem with
a string that matches its Text.
Is there a known bug with RemoveByKey ?
thanks, Bill
Morten Wennevik [C# MVP] - 14 Mar 2008 14:57 GMT
Hi Bill,
RemoveByKey is actually 'RemoveByName' as Key in this case is matched
against the Name property.
If you add menuitems dynamically remember to set the MenuItem.Name property,
the Visual Studio designer will put the reference name in this property if
you create the menuitem in the designer.

Signature
Happy Coding!
Morten Wennevik [C# MVP]
> ..NET 2.0 C#
>
[quoted text clipped - 26 lines]
>
> thanks, Bill
Bill Woodruff - 16 Mar 2008 09:07 GMT
Morten wrote :
"RemoveByKey is actually 'RemoveByName' as Key in this case is matched
against the Name property.
If you add menuitems dynamically remember to set the MenuItem.Name property,
the Visual Studio designer will put the reference name in this property if
you create the menuitem in the designer."
Hi Morten,
Thanks very much for taking your time to reply on this. I had worked it out
myself, the same day you posted, and felt rather chagrined that I could have
forgotten something simple like this that I had dealt with once before.
I deeply appreciate the responses of you and the other MVP's and "gurus"
here !
best, Bill