0
votes

I'm working on a comboBox XML Ribbon Control and I'm going crazy to obtain the index of the selected item.

This is the Ribbon XML code with the comboBox:

<customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui">
<ribbon>
  <tabs>

    <tab id="SearchCustomerTab" insertAfterMso="TabAddIns" label="Cliente" visible="true">
      <group id="SearchCustomerGroup" label="Cliente" autoScale="true">
        <comboBox id="CustomerComboBox" getItemCount="GetItemCountCallback" getItemLabel="GetItemLabelCallback" getItemID="GetItemIDCallback" onChange="OnChangeCallback" />
      </group>
    </tab>
  </tabs>
 </ribbon>
</customUI>

With getItemCount and getItemLabel callback I correctly fill che comboBox (oTabCustomersList is a List of a custom class):

Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer
    Return oTabCustomersList.Count

End Function

Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return oTabCustomersList(index).NomeCompleto

End Function

With getItemId callback I set the index of every item in the ID:

Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return index.ToString

End Function

but with onChange callback I can obtain the item label but not the ID or the selected index:

Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)

    Debug.WriteLine("OnChangeCallback text: " & text) 'text = item label

End Sub

Is there a way to obtain the index of the selected item with the Ribbon comboBox control?

Thanks in advance,

Simone

1
Could you please add a minimal reproducible example that includes the minimum Ribbon XML and any code necessary to the control when the add-in loads so that we don't have to spend time setting this up for testing?Cindy Meister
Added further code as requestedSimone

1 Answers

1
votes

Unfortunately it's not possibile to get the index of the selection in a ribbon comboBox (source)

Whenever the value of the combo box is selected, the onChange callback receives the text. However, it is not possible to get the index of the selection.

I've solved using a Dictionary (of String, CustomClass) where string is the text parameter of OnChangeCallback:

Private customClass As CustomClass
Private customDictionary As Dictionary(Of String, CustomClass)

Public Sub Ribbon_Load(ByVal ribbonUI As Office.IRibbonUI)
    Dim customList As List(Of CustomClass)

    customList = FunctionToPopulateMyList()
    customDictionary = customList.ToDictionary(Function(p) p.MyText, Function(p) p)

End Sub

Public Function GetItemLabelCallback(ByVal control As Office.IRibbonControl, index As Integer) As String

        Return oCustomDictionary.ElementAt(index).Value.MyText
End Function

Public Function GetItemCountCallback(ByVal control As Office.IRibbonControl) As Integer

        Return oCustomDictionary.Count

End Function

Public Function GetItemIDCallback(ByVal control As Office.IRibbonControl, index As Integer) As String
    Return "Item" & index.ToString & control.Id

End Function

Public Sub OnChangeCallback(ByVal control As Office.IRibbonControl, text As String)
    If (customDictionary.ContainsKey(text)) Then
        customClass = customDictionary(text)

    End If
End Function