Programmatically selecting the item on dynamic Dropdown list in ribbon bar.

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

Programmatically selecting the item on dynamic Dropdown list in ribbon bar.
 
Glen Lewis




Posts: 29
Joined: 2021-03-12
I have been trying to get drop down list to show the selected item after I build the Ribbon Item.

I have the code below of how I am building the dropdown which works perfectly. But when I assign the selectedItemId. The ribbon bar doesn't reflect the selection. It stays blank.

I added the
adxCompanyDD.Invalidate();
which I read should correct this issue I am having.

Please help me here, I have been trying to get this to work for a while now.


https://www.add-in-express.com/forum/read.php?FID=5&TID=11667


                    ADXRibbonItem selectedItem = null;
                    adxCompanyDD.Items.Clear();
                    
                    //Which one do I need to set to not default?
                    adxCompanyDD.SelectedItemIndex = 0;
                    adxCompanyDD.SelectedItemId = "";
                    //End of question 1

                    foreach (var workspace in workspaces.List)
                    {
                        var adxRibbonItem = new ADXRibbonItem
                        {
                            Caption = workspace.Name,
                            Id = String.Format("adxCompanyDD_{0}", workspace.WorkspaceId),
                            ImageTransparentColor = System.Drawing.Color.Transparent
                        };

                        var optionItem = adxCompanyDD.Items.Add(adxRibbonItem);

                        //get the selected item and assign it if matches correct workspace id.
                        if (_UserSettings.WorkspaceId == workspace.WorkspaceId)
                            selectedItem = adxRibbonItem;
                    }

                    if (selectedItem != null)
                    {
                        adxCompanyDD.SelectedItemId = selectedItem.Id;
                        adxCompanyDD.SelectedItemIndex = 1;
                    }

                    var group = adxCompanyDD.Collection.Owner as ADXRibbonGroup;
                    var tab = group.Collection.Owner as ADXRibbonTab;
                    adxCompanyDD.Invalidate();
                    tab.Invalidate();
Posted 22 Dec, 2022 16:06:18 Top
Andrei Smolin


Add-in Express team


Posts: 18816
Joined: 2006-05-11
Hello Glen,

Here's a project demonstrating the approach described in that post. There are two drop downs (ADXRibbonDropDown; note that this approach can be used with ADXRibbonGallery) both populated with three items. The items' captions are "0", "1", and "2" in both cases. I've changed the item ids on one of the drop downs to "Zero", "One" and "Two"; I will call this drop down "Id-based"; the other one is "Index-based". NOTE: If you change the ID of a Ribbon control, make sure the new ID is unique across the whole set of Ribbon controls in your add-in.

The default values are set as follows:
Id-based drop down:
- SelectedItemIndex = -1. This is the default value; I left it unchanged.
- SelectedItemId: "Two". That is, at startup the drop down shows the last item

Index-based drop down:
- SelectedItemIndex = 2. That is, at startup the drop down shows the last item
- SelectedItemId: {empty}. This is the default value; I left it unchanged.

Also, I added two Ribbon buttons that set SelectedItemIndex on the Index-based drop down and SelectedItemId on the Id-based one. Note that I don't call Invalidate because Add-in Express invokes Invalidate() whenever you set any property of a Ribbon component.

https://temp.add-in-express.com/support/ADXRibbonDropDown_DefaultValue_SelectedItemProgrammatically.zip

Regards from Poland (GMT+1),

Andrei Smolin
Add-in Express Team Leader
Posted 23 Dec, 2022 08:12:17 Top
Glen Lewis




Posts: 29
Joined: 2021-03-12
I reviewed your sample you sent me, yours is working on an event. Is there different behavior when it isn't an event changing the selected item?

I have my combo being created from the "AddinModule_AddinInitialize" event as you can see below. Then I am calling a method to generate the combo and then set the "SelectedItemId" based on the Value in the UserSettings class.

I removed the invalidate calls. Still, I am not having any success to making this work.

I am storing the ItemId in the for each loop as I create the ADXRibbonItem when the workspaceId matches the for each object. So, I can set it after the items have been added.

I then set the "adxCompanyDD.SelectedItemId = selectedItem.Id;".

Still when the excel loads the ribbon bar is still showing a blank in the dropdown box.

User added an image
User added an image

As you can see from the image, the dropdown is still not showing a value or caption of the selected item.


Thank you for your quick response today.



 private void AddinModule_AddinInitialize(object sender, EventArgs e)
        {
            _UserSettings = new UserSettings(true);
            InitWorkspaceDropDown();
            SetRibbonBarControls();
        }



        private void InitWorkspaceDropDown()
        {

            if (_UserSettings.Workspaces == null)
                return;
            if (_UserSettings.Workspaces.List == null)
                return;

            _UserSettings.Workspaces.GetAPIResults(_UserSettings);
            var workspaces = _UserSettings?.Workspaces;
            if (workspaces.List.Any())
            {
                ADXRibbonItem selectedItem = null;
                adxCompanyDD.Items.Clear();

                foreach (var workspace in workspaces.List)
                {
                    var adxRibbonItem = new ADXRibbonItem
                    {
                        Caption = workspace.Name,
                        Id = String.Format("adxCompanyDD_{0}", workspace.WorkspaceId),
                        ImageTransparentColor = System.Drawing.Color.Transparent
                    };

                    var optionItem = adxCompanyDD.Items.Add(adxRibbonItem);

                    //get the selected item and assign it if matches correct workspace id.
                    if (_UserSettings.WorkspaceId == workspace.WorkspaceId)
                        selectedItem = adxRibbonItem;
                }

                if (selectedItem != null)
                {
                    adxCompanyDD.SelectedItemId = selectedItem.Id;
                }
            }
        }
Posted 23 Dec, 2022 12:58:23 Top
Glen Lewis




Posts: 29
Joined: 2021-03-12
I figured out what I was doing wrong and understanding what you were telling me.

Within the properties of the dropdown I must give it an initial value for the SelectedItemId this sets it up to use that value when I change the value later.

Thank you so much, I finally copied your example and looked at the property difference and that is what helped me resolve my issue.

User added an image

Once I set this value in the ADX Dropdown all worked as expected.


Thanks again.
Posted 23 Dec, 2022 14:04:18 Top