I am trying to conditionally switch between a Command Bar Button and a Command Bar Drop-Down Button with this code:
if (A)
{
if (commandBarStripElement1.Items.Contains(cbbCopy))
{
location = commandBarStripElement1.Items.IndexOf(cbbCopy);
commandBarStripElement1.Items[location] = cbDropDownButtonCopy;
}
}
else
{
if (commandBarStripElement1.Items.Contains(cbDropDownButtonCopy))
{
location = commandBarStripElement1.Items.IndexOf(cbDropDownButtonCopy);
commandBarStripElement1.Items[location] = cbbCopy;
}
The code works fine when A is true, but the second time the function is entered and A is false. I get this error:
System.InvalidOperationException: Element already added
The error is thrown from this line:
commandBarStripElement1.Items[location] = cbbCopy;
I've debugged in and checked all of the items in commandBarStripElement1 and cbbCopy is not in there. Is there some other way to swap items in the Command Bar while maintaining their position?