Getting the selected item from the dropdown in a r

2020-08-02 05:54发布

问题:

I see the following question which explains how to get the selected item from the dropdown:-

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d1cf7b3e-68cf-4b82-b806-a3431acde3b6/

The above thread advises to have a hashtable of the items upfront, cache the selected id in the onAction() of the dropdown and use that selected id to find the item from the hashtable in the onAction() of the button.

BUT, in my case, I populate the ribbon XML from the database. This ribbon XML obviously has the XML for dropdown and I am useing macros to interact with the dropdown and other controls in the ribbon. I am not sure how to have a collection upfront which can be used by the macro similar to approach described in the above thread.

回答1:

I wanted to put the solution just in case someone has the same problem:-

This is how my ribbon dropdown looks like :-

    <dropDown id="ddlItem"  
 getItemLabel="SetTheSelectedItemInDropDown"
 onAction="GetTheSelectedItemInDropDown"  label="Items">
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    </dropDown>

Note the callbacks for getItemLabel and onAction. Interestingly, getItemLabel is meant for setting the item on the dropdown (get by the dropdown). It is kinda confusing but thats the way it is and thats why I named my method as "SetTheSelectedItemInDropDown".

Function "GetTheSelectedItemInDropDown" for the onAction is to get the selected item.

Now following is the macro code:-

' Declare a global variable to hold the selected item
 Dim itemName As String


' Definition of GetTheSelectedItemInDropDown which gets the selected item of the dropdown
 Sub GetTheSelectedItemInDropDown(control As IRibbonControl
 , id As String, index As Integer)
  If control.id = "ddlItems" Then
  itemName= id
  End If
 End Sub


 'Definition for SetTheSelectedItemInDropDown which sets the value in the dropdown from the global variable
 Sub SetTheSelectedItemInDropDown(control As IRibbonControl,
  index As Integer, ByRef returnedVal)
  If control.id = "ddlItems" Then
  returnedVal = itemName
  End If
 End Sub

And thats it, you should be able to set and get the dropdown now.



回答2:

Next function script is for Excel... after 2 hour testing and google searching, i´ve found a way how to change and get value.

1.) You must have list of items (sequence), witch are in dropdowns.

2.) use .onaaction and application.caller features: (sorry for czech language in next script):

Sub test1()
Dim zabka As Byte
zabka = ActiveSheet.DropDowns((Application.Caller)).Value
'MsgBox zabka
Select Case zabka
Case 1
ActiveSheet.DropDowns((Application.Caller)).Text = "předání signální paré"
Case 2
ActiveSheet.DropDowns((Application.Caller)).Text = "předání čistopis"
Case 3
ActiveSheet.DropDowns((Application.Caller)).Text = "předání dokumentace SP"
End Select

End Sub

Sub aha()

With ActiveSheet.DropDowns.Add(Left:=Range("B" & 11 - 1).Left + 27, Top:=Range("B" & 11 -      1).Top, Width:=113, Height:=14)

.Caption = ""
.Name = "251"
.Text = "hoho"
.AddItem "předání signální paré"
.AddItem "předání čistopis"
.AddItem "předání dokumentace SP"
.OnAction = "test1"

'"'test1 " & xy & " '" <-way to call a sub and give a variable
End With
End Sub