Replacing a button in the Command Bar

3 posts, 1 answers
  1. Paul
    Paul avatar
    23 posts
    Member since:
    Feb 2013

    Posted 24 May 2013 Link to this post

    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. Answer
    Paul
    Paul avatar
    23 posts
    Member since:
    Feb 2013

    Posted 28 May 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 29 May 2013 Link to this post

    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?
Back to Top