Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1040
inherited toolbar
posted

I create usercontol and inherited Infragistics.Win.UltraWinToolbars.UltraToolbarsManager.

And I create <Browsable> propery. [UseRibbonStyle]
Set the "True" ->  It was create a ribbon tool.

but only once show form designer.

I get an error message when I open again form.

Error is Key already exists .

what's the problem?

I think.. there is a problem her.. ..
 Me.Ribbon.Tabs.AddRange(New Infragistics.Win.UltraWinToolbars.RibbonTab() {oRibbonTab(0), oRibbonTab(1), oRibbonTab(2), oRibbonTab(3)})

Attach a source file.

RibbonToolbar.zip
  • 469350
    Verified Answer
    Offline posted

    Hi,

    Inheriting from any control that contains collections (like the tabs, groups, or tools collection of a toolbar) is going to be very tricky. I strongly advise against doing this, or at least don't add any items to the collections at design-time. This is not just true for Infragistics controls, but any control that has a collection property. For example, the inbox TreeView control will display some very strange behavior similar to what you are experiencing here.

    Having said that, here's a detailed explanation of exactly what's happening here and how you can avoid it:

    What you probably did in this case is you place an instance of your derived toolbar component on the form and you set the UseRibbonStyle property to true.

    At this point, the Form designer in DotNet is dirty. When you the form, the property value you set gets serialized into the Form1.Designer.vb file. But that's not all that get serialized. The Tools and Tabs collection of the ToolbarsManager are also public. So all of the tabs, groups, tools, etc. that you added to the component are serialized, as well. You can see this in the designer code.

        '
            'MyRibbonToolbar1
            '
            Me.MyRibbonToolbar1.DesignerFlags = 1
            Me.MyRibbonToolbar1.DockWithinContainer = Me
            Me.MyRibbonToolbar1.DockWithinContainerBaseType = GetType(System.Windows.Forms.Form)
            Me.MyRibbonToolbar1.UseRibbonStyle = RibbonToolbar.MyRibbonToolbar.ENUM_TRUEFALSE.[TRUE]
            ...
            Me.MyRibbonToolbar1.Ribbon.NonInheritedRibbonTabs.AddRange(New Infragistics.Win.UltraWinToolbars.RibbonTab() {RibbonTab1, RibbonTab2, RibbonTab3, RibbonTab4})
        ...
            Me.MyRibbonToolbar1.Tools.AddRange(New Infragistics.Win.UltraWinToolbars.ToolBase() {ButtonTool4, ButtonTool5, ButtonTool6})

    Now imagine what happens when this code is executed. The designer code sets the UseRibbonStyle property first, and then it's trying to deserialize the tools, which already exist, because they were added when you set the UseRibbonStyle.

    There's no simple way around this.

    The only way to make it work would be to delay adding the groups, tools, etc. until after the deserialization of the tools is complete. Also, you would need to update the SetRibbonStyle method so that it checks to see if the groups and such already exist before it tries to add them. Presumably, if the user makes changes to those tools, you wouldn't want to delete them and re-add them, anyway.

    To introduce the delay, you could make use of the ISupportInitialize interface, which the ToolbarsManager already implements.

    This is a bit tricky, so I modified your sample to use this approach and I am posted it here for you to use as a guideline. I commented my changes using "*** Infragistics"so they are easy to find. But here's a list of what I changed.

    1) Handle OnBeginInit - we need to set a flag here so we know we are initializing.

    2) Hand OnEndInit - we need this to clear the flag we set in OnBeginInit and also to call SetRibbonStyle after the Initialization is complete.

    3) Check the flag in the UseRibbonStyle setter. This is so we skip the code while initializing. Alternately, this check could just as easily go inside the SetRibbonStyle method.

    4) I added a check in SetRibbonStyle to bail out if there are any existing tabs in the ribbon. This is just quick and dirty. You will may want to make this more specific and look for specific tabs, groups, or tools by key.

    RibbonToolbar.zip