This is a migrated thread and some comments may be shown as answers.

Replacing a button in the Command Bar

2 Answers 114 Views
CommandBar
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 24 May 2013, 10:14 PM

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?

 

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Paul
Top achievements
Rank 1
answered on 28 May 2013, 07:43 PM
I accomplished what I wanted by setting the button's Visibility to ElementVisibility.Collapsed instead of adding and removing the buttons. If there is a better way to handle this, please let me know.
0
Stefan
Telerik team
answered on 29 May 2013, 10:58 AM
Hi Paul,

I can confirm this is a better way to do that and in fact this is what I was going to suggest.

Should you have any other questions, do not hesitate to contact us.
 

Regards,
Stefan
Telerik
RadChart for WinForms is obsolete. Now what?
Tags
CommandBar
Asked by
Paul
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or