How to unselect a dropdown in the Ribbon (2 Viewers)

tvanstiphout

Active member
Local time
Today, 02:54
Joined
Jan 22, 2016
Messages
222
NW2 Dev Edition has a ribbon with a dropdown showing MRU items. I'm re-using that concept in my own app.
When you drop it down and select the same item as is already selected, the Change event does not happen; no event happens whatsoever.
If it was a textbox, you could Invalidate the control and set it to "", so the same value can be entered again, but no such luck with a dropdown.
I tried returning Null or -1 or nothing at all from the GetSelectedItemIndex callback, but the first two result in errors, the third does not help (first item is selected).
Is there indeed no way to re-select the same item and respond to that action? Not even a slimy hack?

1710797592454.png
 

isladogs

MVP / VIP
Local time
Today, 10:54
Joined
Jan 14, 2017
Messages
18,225
I've never had a reason to do this but how about first clearing the existing entry on before update?
 

isladogs

MVP / VIP
Local time
Today, 10:54
Joined
Jan 14, 2017
Messages
18,225
Oops. That falls into the category of the last sentence in my signature line. :rolleyes:

So I asked CoPilot which told me:

Clearing a dropdown in the ribbon using VBA can be achieved through different approaches. Let’s explore a couple of methods:

  1. Using Callbacks in Custom Ribbon XML:
    • If you have a custom ribbon with a dropdown, you can add a callback to clear the selected item. Here’s an example:
      ' Add the following callback in your VBA code:
      Sub CallbackDDGetSelectedItemID(control As IRibbonControl, ByRef itemID)
      ' Callback to get the selected item for the dropdown control
      Select Case control.ID
      Case "myDropDown" ' Replace with your actual dropdown ID
      itemID = "" ' Clear the selected item
      End Select
      End Sub

      ' Then call this in the dropdown's OnAction event:
      objRibbon.InvalidateControl("myDropDown")

    • Replace "myDropDown" with the actual ID of your dropdown. This approach invalidates the control, effectively clearing the selection 1.
  2. Direct Access to Ribbon Element Values:
    • If you want to access the dropdown element values directly, you can use callbacks like GetSelectedItemIndex, GetItemLabel, and GetItemID. For instance:
      Sub GetSelectedItemIndex(control As IRibbonControl, ByRef returnedVal)
      ' Returns the index position of the selected item from the dropdown control
      If control.ID = "Drop" Then
      MsgBox returnedVal
      End If
      End Sub

      Sub GetItemLabel(control As IRibbonControl, Index As Integer, ByRef returnedVal)
      ' Returns the label of the selected item from a ribbon dropdown control
      ' Implement similar logic for GetItemID
      ' ...
      End Sub

    • You can pass a control object from one callback to another, manually invoking these procedures 2.

Anything of use there? Or is that equally useless?
 

tvanstiphout

Active member
Local time
Today, 02:54
Joined
Jan 22, 2016
Messages
222
Plausible, but did not work. Returning Null or "" or -1 from GetSelectedItemID callback gives the same error as when using GetSelectedItemIndex: The callback "..." returned a value that could not be converted to the expected type.
or
The callback "..." returned an invalid value.
I'm thinking of a workaround of adding "--- Select a value ---" to the dropdown, and have it at Index=0.
 

GPGeorge

Grover Park George
Local time
Today, 02:54
Joined
Nov 25, 2004
Messages
1,867
Plausible, but did not work. Returning Null or "" or -1 from GetSelectedItemID callback gives the same error as when using GetSelectedItemIndex: The callback "..." returned a value that could not be converted to the expected type.
or
The callback "..." returned an invalid value.
I'm thinking of a workaround of adding "--- Select a value ---" to the dropdown, and have it at Index=0.
I have used a method similar to that in Access combo boxes. But I tend to be lazy, so there's that to consider.
 

Users who are viewing this thread

Top Bottom