如何在自定义下拉功能区控件上设置所选项目 [英] How to set selected item on Custom DropDown Ribbon Control
问题描述
我正在使用自定义UI编辑器"为Excel创建自定义"标签,并且其中有两个DropDown控件.我们称它们为DropDown1和DropDown2.我的目标是,每当更改DropDown1选择时,它都会自动更改DropDown2选择,但是我不知道如何在DropDown控件中设置"SelectedItem".
I'm making a Custom Tab for Excel with Custom UI Editor and I have two DropDown controls in it. Let's call them DropDown1 and DropDown2. My goal is that whenever I change the DropDown1 selection it automatically changes de DropDown2 selection, but I don't know how to set the "SelectedItem" in a DropDown Control.
到目前为止,我具有每次更改DropDown1的选择都会触发的VBA功能,我认为这可能会有所帮助.
So far I have a VBA function which is triggered every time I change the selection of DropDown1, I think that can be helpfull.
推荐答案
您需要在自定义UI编辑器"中向功能区XML添加回调函数,然后将相应的代码添加至要在功能区选项卡中调用的VBA项目.变得无效.您需要为下拉菜单设置所选项目的回调是getSelectedItemIndex
或getSelectedItemID
,具体取决于您是要按索引还是按ID选择项目.由于您没有提供任何代码,因此我的职责是一般性的(未经测试):
You need to add a callback function to you ribbon XML in the Custom UI Editor and then add the corresponding code to you VBA project to be called when the ribbon tab gets invalidated. The callback you need to set the selected item for the dropdown control is either getSelectedItemIndex
or getSelectedItemID
, depending on if you want to select the item by index or by id. Since you have not provided any code, my examle is general (and not tested):
功能区XML :
<dropDown id="drpTest" label="Test dropdown" getSelectedItemIndex="drpTestGetSelectedItem" ></dropDown>
VBA回调
'Callback for drpTest getSelectedItemIndex
Sub drpTestGetSelectedItem(control As IRibbonControl, ByRef returnedVal)
returnedVal = 1 '***** To select the item with index 1,
'***** replace with code to select the desired item
End Sub
编辑:
根据其他下拉列表选择索引的示例.在类似的解决方案中,我已经在一个控件的onAction
函数中设置了一个值,并使用它在另一个控件中设置了选定的索引,如下所示:
Example where index is selected based on other droplist. In similar solutions I have set a value in the onAction
function of one control and used it to set the selected index in another control, something like the following:
功能区XML :
<dropDown id="drpTest1" label="Test dropdown 1" onAction="drpTest1OnAction" ></dropDown>
<dropDown id="drpTest2" label="Test dropdown 2" getSelectedItemIndex="drpTest2GetSelectedItem" ></dropDown>
VBA回调
Global myRibbon As IRibbonUI
Global giIndex As Integer
'Callback for customUI.onLoad
Sub RibbonOnLoad(ribbon As IRibbonUI)
'***** Save reference to ribbon object to invalidate
Set myRibbon = ribbon
End Sub
'Callback for drpTest1 onAction
Sub drpTest1OnAction(control As IRibbonControl, id As String, index As Integer)
'***** Set selected item variable for drpTest2
giIndex = index
'***** Tell Excel to redraw ribbon
'(you could invalidate only parts of the ribbon with InvalidateControl
'or InvalidateControlMso)
myRibbon.Invalidate
End Sub
'Callback for drpTest2 getSelectedItemIndex
Sub drpTest2GetSelectedItem(control As IRibbonControl, ByRef returnedVal)
'***** Return selected item for drpTest2 based on value stored in giIndex
returnedVal = giIndex
End Sub
这篇关于如何在自定义下拉功能区控件上设置所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!