Adding ADXCommandBarButton at Run-Time

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

Adding ADXCommandBarButton at Run-Time
Tyson Stolarski

Posts: 38
Joined: 2006-12-01

I've been searching for a way to add buttons to commandbars at run time.

I did find this previous topic, with a good example, here:

However, in that example, it would create a new ADXControl every time. What Im looking at doing is examining the right click event, and then based on some certain conditions, I will add 3 buttons to the context menu. If the conditions arn't true, and the buttons exist from a previous right click, I will have to remove them.

Now rather than creating and deleting them everytime, which gets messy and possibly prone to errors, it would be much more effecient to keep 3 references around to them, and simple add and remove them from the ADXCommandBar based on the right click event. However, whenever I attempt this, it never displays the buttons. What is the trick with the example mentioned above that makes it display correctly???


Posted 19 Jan, 2007 01:40:23 Top
Tyson Stolarski

Posts: 38
Joined: 2006-12-01
Below is an example of my code. The first time it runs, the button doesnt exist in the cache, so it creates it. It displays fine after that first creation. However, once its removed elsewhere in my code, and then added back with the same code below (but this time it just grabs it out of the cache) it doesnt display. As you can see below I have tried playing around with various properties, seeing if they force a refresh, but with no luck.

note: used ( instead of < for the generics as it was getting confused with the html tags.

private Dictionary(string, ADXCommandBarButton) buttonCache = new Dictionary(string, ADXCommandBarButton)();

private ADXCommandBarButton AddADXButton( ADXCommandBar container, string caption, string controlTag, int before,
                                                  bool beginGroup, ADXClick_EventHandler eventHandler )
            if (!buttonCache.ContainsKey(controlTag))
                ADXCommandBarControl adxNewControl;
                ADXBaseControlCollection collection = container.Controls;

                if (eventHandler != null)
                    adxNewControl = collection.GetControlByControlTag(controlTag, false);
                    if (adxNewControl != null)
                        adxNewControl.AsButton.Click -= eventHandler;

                adxNewControl = collection.Add(typeof(ADXCommandBarButton), controlTag, 1, before, false);
                adxNewControl.BeginGroup = beginGroup;
                adxNewControl.Caption = caption;

                adxNewControl.AsButton.Style = ADXMsoButtonStyle.adxMsoButtonCaption;
                if (eventHandler != null)
                    adxNewControl.AsButton.Click += eventHandler;

                buttonCache.Add(controlTag, adxNewControl.AsButton);
                ADXCommandBarButton adxButton = buttonCache[controlTag];

                // Workaround so that Excel refreshes its toolbar GUI - Doesnt Work!!!.
                adxButton.Caption = "";
                adxButton.Caption = caption;

            return buttonCache[controlTag];

Or is it a problem with the container.Controls.Add(adxButton) method???
Do you have to use the collection.Add(typeof(ADXCommandBarButton), controlTag, 1, before, false) method, so that a new button is created and then the UI is refreshed???
Posted 19 Jan, 2007 02:31:27 Top
Sergey Grischenko

Add-in Express team

Posts: 7233
Joined: 2004-07-05
Hi Tyson.

Please download the following example:

The code creates new controls or connects to them if they are already exist.

P.S. Note that we take up your forum requests in the order we receive them.
Besides, it may take us some time to investigate your issue. Please be sure we will let you know as soon as the best possible solution is found.
Posted 19 Jan, 2007 07:55:06 Top