Updating a dynamic menu

Add-in Express™ Support Service
That's what is more important than anything else

Updating a dynamic menu
 
Kristian Guttesen




Posts: 39
Joined: 2011-11-03
Hello,

I'm in the process of creating a dynamic ribbon menu (ADXRibbonMenu with the 'Dynamic' attribute set). However, I've run into a bit of trouble making the menu upd ate correctly, and I haven't found any documentation on proper usage.

The code works as follows:
* In the 'OnRibbonBeforeCre ate' event, the menu is populated with ADXRibbonButtons
* Once ribbon has been created, it displays the menu items correctly
* At some point later, I clear the ADXRibbonMenu control collection and add a modified button collection:
rxmenuX.Controls.Clear()
For Each oButton In listButtons
    rxmenuX.Controls.Add(oButton)
Next

...and I invalidate the entire ribbon afterwards.

* The menu items are displaying incorrectly or in a different order than the control list added to the 'Controls' collection

Besides, new controls that have are added to the dynamic menu and updated afterwards are missing their label - until I add the following to the 'OnCre ate' event:
Private Sub rxmenuX_OnCreate(sender As System.Object, e As AddinExpress.MSO.ADXRibbonCreateMenuEventArgs) Handles rxmenuX.OnCreate
        For Each oButton As AddinExpress.MSO.ADXRibbonButton In rxmenuX.Controls
            e.AddControl(oButton)
        Next
    End Sub



Can you point me in the right direction as to how the ADXRibbonMenu can be updated dynamically with any modified se t of ADXRibbonButton controls?

Thanks in advance.
Posted 03 Nov, 2011 09:30:57 Top
Eugene Astafiev


Guest


Hi Kristian,

Please note that the Ribbon UI is a static thing from its birth. You can read more about this in the How Office Ribbon controls are created? and Creating Ribbon controls at run-time sections of the http://www.add-in-express.com/docs/net-ribbon-components.php. It states:

The only control providing any dynamism is Dynamic Menu if the ADXRibbonMenu.Dynamic property is set to True at design-time, the component will generate the OnCreate event allowing creating menu items at run-time.


I.e. you need to create and add your new menu items in the OnCreate event handler:

private void adxRibbonMenu1_OnCreate(object sender, ADXRibbonCreateMenuEventArgs e)
{
    e.Clear();
    AddinExpress.MSO.ADXRibbonButton btn = new ADXRibbonButton();
    btn.Caption = "adxRibbonButton1";
    btn.Id = "adxRibbonButton_6a40342fbc484a72b9f520854d27a821";
    btn.ImageTransparentColor = System.Drawing.Color.Transparent;
    btn.Ribbons = ((AddinExpress.MSO.ADXRibbons)((AddinExpress.MSO.ADXRibbons.msrOutlookMailRead | AddinExpress.MSO.ADXRibbons.msrOutlookMailCompose)));
    e.AddControl(btn);
}


Does this code work as expected?
Posted 03 Nov, 2011 10:55:57 Top
KristianG




Posts: 39
Joined: 2011-11-03
Thank you very much, Eugene. I understand that using the Controls collection of the dynamic menu before the OnCre ate event is improper usage.

Instead of adding the controls to the Controls collection of the dynamic menu in the main event, I now use a control list:
Dim ctlist As New List(Of AddinExpress.MSO.ADXRibbonButton)


In the OnCre ate event, I modified your code to add the controls from my declared control list to ADXRibbonCreateMenuEventArgs instead of the button you specified:
e.Clear()
For Each oButton ...
e.AddControl(oButton) ...


And everything works as desired!
Posted 04 Nov, 2011 03:23:52 Top
Eugene Astafiev


Guest


Good news, Kristian!

Thank you for letting me know and good luck with your add-in project!
Posted 04 Nov, 2011 04:37:31 Top